Skip to content

Commit

Permalink
Add "Paste current date and time" predefined command
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Aug 6, 2017
1 parent d52e682 commit b39430f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/gui/addcommanddialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "platform/platformnativeinterface.h"

#include <QAbstractListModel>
#include <QLocale>
#include <QSortFilterProxyModel>

Q_DECLARE_METATYPE(Command)
Expand All @@ -43,6 +44,24 @@ write('image/png', imageData)
copy('image/png', imageData)
)";

constexpr auto commandPasteDateTimeTemplate = R"(
// http://doc.qt.io/qt-5/qdatetime.html#toString
var format = '%1'
var dateTime = dateString(format)
copy(dateTime)
paste()
)";

QString commandPasteDateTime()
{
const auto format = QLocale::system().dateTimeFormat(QLocale::LongFormat)
.replace("\\", "\\\\")
.replace("'", "\\'")
.replace("\n", "\\n");

return QString(commandPasteDateTimeTemplate).arg(format);
}

Command *newCommand(QList<Command> *commands)
{
commands->append(Command());
Expand Down Expand Up @@ -75,7 +94,8 @@ enum GlobalAction {
GlobalActionEnableClipboardStoring,
GlobalActionPasteAndCopyNext,
GlobalActionPasteAndCopyPrevious,
GlobalActionScreenshot
GlobalActionScreenshot,
GlobalActionPasteDateTime
};

void createGlobalShortcut(const QString &name, const QString &script, IconId icon,
Expand Down Expand Up @@ -123,6 +143,8 @@ void createGlobalShortcut(GlobalAction id, Command *c, const QStringList &s = QS
createGlobalShortcut( AddCommandDialog::tr("Paste and copy previous"), "paste(); previous()", IconArrowCircleOUp, s, c );
else if (id == GlobalActionScreenshot)
createGlobalShortcut( AddCommandDialog::tr("Take screenshot"), commandScreenshot, IconCamera, s, c );
else if (id == GlobalActionPasteDateTime)
createGlobalShortcut( AddCommandDialog::tr("Paste current date and time"), commandPasteDateTime(), IconClockO, s, c );
else
Q_ASSERT(false);
}
Expand Down Expand Up @@ -166,6 +188,7 @@ QList<Command> defaultCommands()
createGlobalShortcut(GlobalActionPasteAndCopyNext, &commands);
createGlobalShortcut(GlobalActionPasteAndCopyPrevious, &commands);
createGlobalShortcut(GlobalActionScreenshot, &commands);
createGlobalShortcut(GlobalActionPasteDateTime, &commands);
#endif

c = newCommand(&commands);
Expand Down
1 change: 1 addition & 0 deletions src/gui/icons.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum IconId {
IconTrash = 0xf014,
IconHome = 0xf015,
IconFileAlt = 0xf016,
IconClockO = 0xf017,
IconDownloadAlt = 0xf019,
IconArrowCircleODown = 0xf01a,
IconArrowCircleOUp = 0xf01b,
Expand Down

0 comments on commit b39430f

Please sign in to comment.