From 17f089f9fbcbfaf576e4b8aa0ae343f0bf6c481e Mon Sep 17 00:00:00 2001 From: "Peter Savichev (proton)" Date: Sun, 9 Jun 2013 15:20:17 +0400 Subject: [PATCH] added automatic shortcut help generation --- aboutDialog.cpp | 16 ++++-------- mainwindow.cpp | 67 +++++++++++++++++++++++++------------------------ mainwindow.h | 9 ++----- shared.cpp | 18 +++++++++++++ shared.h | 15 +++++++++++ znotes.pro | 6 +++-- 6 files changed, 78 insertions(+), 53 deletions(-) create mode 100644 shared.cpp create mode 100644 shared.h diff --git a/aboutDialog.cpp b/aboutDialog.cpp index 7f58e44..7d58dea 100644 --- a/aboutDialog.cpp +++ b/aboutDialog.cpp @@ -1,5 +1,6 @@ #include "aboutDialog.h" #include "ui_aboutDialog.h" +#include "shared.h" //Fixed problems with compilation, if program version is undefined #ifndef VERSION @@ -70,17 +71,10 @@ aboutDialog::aboutDialog(QWidget *parent) : QString strHelp; strHelp.append(QString("

%1

").arg(tr("Shortcuts:"))); strHelp.append(QString("%1 - %2
").arg("Alt+Fn").arg(tr("Go to note n"))); - strHelp.append(QString("%1 - %2
").arg("Alt+Left").arg(tr("Go to previous note"))); - strHelp.append(QString("%1 - %2
").arg("Alt+Right").arg(tr("Go to next note"))); - strHelp.append(QString("%1 - %2
").arg("Ctrl+N").arg(tr("Create new note"))); - strHelp.append(QString("%1 - %2
").arg("Ctrl+W").arg(tr("Remove current note"))); - strHelp.append(QString("%1 - %2
").arg("F2").arg(tr("Rename current note"))); - strHelp.append(QString("%1 - %2
").arg("Ctrl+F").arg(tr("Search in the notes' text"))); - strHelp.append(QString("%1 - %2
").arg("Ctrl+B").arg(tr("Make selected text bold"))); - strHelp.append(QString("%1 - %2
").arg("Ctrl+I").arg(tr("Make selected text italic"))); - strHelp.append(QString("%1 - %2
").arg("Ctrl+S").arg(tr("Make selected text strikeout"))); - strHelp.append(QString("%1 - %2
").arg("Ctrl+U").arg(tr("Make selected text underline"))); - strHelp.append(QString("%1 - %2
").arg("Ctrl+Q").arg(tr("Exit program"))); + QShortcut* shortcut; + foreach(shortcut, Shared::shortcuts()) + if(!shortcut->whatsThis().isEmpty()) + strHelp.append(QString("%1 - %2
").arg(shortcut->key().toString()).arg(shortcut->whatsThis())); ui->lbHelp->setHtml(strHelp); //adjustSize(); } diff --git a/mainwindow.cpp b/mainwindow.cpp index 0244217..2c998c7 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -5,6 +5,7 @@ #include "aboutDialog.h" #include "note_html.h" #include "notecreatewidget.h" +#include "shared.h" #include #include @@ -145,8 +146,9 @@ void MainWindow::closeEvent(QCloseEvent *event) } } -void MainWindow::moveEvent(QMoveEvent *event) +void MainWindow::moveEvent(QMoveEvent* event) { + Q_UNUSED(event) if(note_create_widget && note_create_widget->isVisible()) { // TODO: Check if the toolbar is on the right side or bottom and 'flip' the note_create_widget @@ -324,6 +326,8 @@ inline QAction* GenerateAction(int item_id /*, bool checkable = false*/) return action; } +#include + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), note_create_widget(0) { @@ -391,38 +395,35 @@ MainWindow::MainWindow(QWidget *parent) tray.show(); //Creating shortcuts -// scAdd = new QShortcut(QKeySequence::New, this); - scAdd = new QShortcut(Qt::CTRL + Qt::Key_N, this); - scRemove = new QShortcut(Qt::CTRL + Qt::Key_W, this); - scRename = new QShortcut(Qt::Key_F2, this); - scBack = new QShortcut(QKeySequence::Back, this); - scForward = new QShortcut(QKeySequence::Forward,this); - scPrev = new QShortcut(QKeySequence::PreviousChild, this); - scNext = new QShortcut(QKeySequence::NextChild,this); - scSearch = new QShortcut(QKeySequence::Find, this); - scSearchEsc = new QShortcut(Qt::Key_Escape, ui->edSearch, 0, 0, Qt::WidgetShortcut); -// scExit = new QShortcut(QKeySequence::Close, this); - scExit = new QShortcut(Qt::CTRL + Qt::Key_Q, this); - scFormatBold = new QShortcut(Qt::CTRL + Qt::Key_B, this); - scFormatItalic = new QShortcut(Qt::CTRL + Qt::Key_I, this); - scFormatStrikeout = new QShortcut(Qt::CTRL + Qt::Key_S, this); - scFormatUnderline = new QShortcut(Qt::CTRL + Qt::Key_U, this); - //Connecting shortcuts with slots - connect(scAdd, SIGNAL(activated()), this, SLOT(NewNote())); - connect(scRemove, SIGNAL(activated()), notes, SLOT(removeCurrentNote())); - connect(scRename, SIGNAL(activated()), notes, SLOT(renameCurrentNote())); - connect(scPrev, SIGNAL(activated()), this, SLOT(PreviousNote())); - connect(scNext, SIGNAL(activated()), this, SLOT(NextNote())); - connect(scBack, SIGNAL(activated()), notes, SLOT(historyBack())); - connect(scForward, SIGNAL(activated()), notes, SLOT(historyForward())); - connect(scSearch, SIGNAL(activated()), actions[itemSearch], SLOT(toggle())); - connect(scSearchEsc, SIGNAL(activated()), actions[itemSearch], SLOT(toggle())); - connect(scExit, SIGNAL(activated()), qApp, SLOT(quit())); - connect(scFormatBold, SIGNAL(activated()), this, SLOT(formatBold())); - connect(scFormatItalic, SIGNAL(activated()), this, SLOT(formatItalic())); - connect(scFormatStrikeout, SIGNAL(activated()), this, SLOT(formatStrikeout())); - connect(scFormatUnderline, SIGNAL(activated()), this, SLOT(formatUnderline())); - // + Shared::addShortcut(new QShortcut(QKeySequence::New, this), tr("Create new note")); + connect(Shared::shortcuts().last(), SIGNAL(activated()), this, SLOT(NewNote())); + Shared::addShortcut(new QShortcut(QKeySequence::Close, this)); + connect(Shared::shortcuts().last(), SIGNAL(activated()), notes, SLOT(removeCurrentNote())); + Shared::addShortcut(new QShortcut(Qt::Key_F2, this), tr("Rename current note")); + connect(Shared::shortcuts().last(), SIGNAL(activated()), notes, SLOT(renameCurrentNote())); + Shared::addShortcut(new QShortcut(QKeySequence::Back, this), tr("Go to previous note")); + connect(Shared::shortcuts().last(), SIGNAL(activated()), this, SLOT(PreviousNote())); + Shared::addShortcut(new QShortcut(QKeySequence::Forward, this), tr("Go to next note")); + connect(Shared::shortcuts().last(), SIGNAL(activated()), this, SLOT(NextNote())); + Shared::addShortcut(new QShortcut(QKeySequence::PreviousChild, this)); + connect(Shared::shortcuts().last(), SIGNAL(activated()), notes, SLOT(historyBack())); + Shared::addShortcut(new QShortcut(QKeySequence::NextChild, this)); + connect(Shared::shortcuts().last(), SIGNAL(activated()), notes, SLOT(historyForward())); + Shared::addShortcut(new QShortcut(QKeySequence::Find, this), tr("Search in the notes' text")); + connect(Shared::shortcuts().last(), SIGNAL(activated()), actions[itemSearch], SLOT(toggle())); + Shared::addShortcut(new QShortcut(Qt::Key_Escape, ui->edSearch, 0, 0, Qt::WidgetShortcut)); + connect(Shared::shortcuts().last(), SIGNAL(activated()), actions[itemSearch], SLOT(toggle())); + Shared::addShortcut(new QShortcut(Qt::CTRL + Qt::Key_Q, this), tr("Exit program")); + connect(Shared::shortcuts().last(), SIGNAL(activated()), qApp, SLOT(quit())); + Shared::addShortcut(new QShortcut(QKeySequence::Bold, this), tr("Make selected text bold")); + connect(Shared::shortcuts().last(), SIGNAL(activated()), this, SLOT(formatBold())); + Shared::addShortcut(new QShortcut(QKeySequence::Italic, this), tr("Make selected text italic")); + connect(Shared::shortcuts().last(), SIGNAL(activated()), this, SLOT(formatItalic())); + Shared::addShortcut(new QShortcut(Qt::CTRL + Qt::Key_S, this), tr("Make selected text strikeout")); + connect(Shared::shortcuts().last(), SIGNAL(activated()), this, SLOT(formatStrikeout())); + Shared::addShortcut(new QShortcut(QKeySequence::Underline, this), tr("Make selected text underline")); + connect(Shared::shortcuts().last(), SIGNAL(activated()), this, SLOT(formatUnderline())); + for(int i=1; i<=9; ++i) //from Alt+1 to Alt+9 { QShortcut* shortcut = new QShortcut(QKeySequence(Qt::ALT + Qt::Key_0+i), this); diff --git a/mainwindow.h b/mainwindow.h index b247bad..8553c1c 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -45,13 +45,8 @@ class MainWindow : public QMainWindow QSignalMapper alt_mapper; //QTimer SaveTimer; // - QAction* actions[itemMax]; -// // - QAction *actShow, *actHide; -// // - QShortcut *scAdd, *scRemove, *scRename, *scBack, *scForward, *scPrev, *scNext, *scExit, *scSearch; - QShortcut *scFormatBold, *scFormatItalic, *scFormatStrikeout, *scFormatUnderline; - QShortcut *scSearchEsc; + QAction* actions[itemMax]; + QAction *actShow, *actHide; // void LoadNotes(); // diff --git a/shared.cpp b/shared.cpp new file mode 100644 index 0000000..2aa86fd --- /dev/null +++ b/shared.cpp @@ -0,0 +1,18 @@ +#include "shared.h" + +QVector p_shortcuts; + +Shared::Shared() +{ +} + +const QVector& Shared::shortcuts() +{ + return p_shortcuts; +} + +void Shared::addShortcut(QShortcut* shortcut, QString comment) +{ + shortcut->setWhatsThis(comment); + p_shortcuts << shortcut; +} diff --git a/shared.h b/shared.h new file mode 100644 index 0000000..e445577 --- /dev/null +++ b/shared.h @@ -0,0 +1,15 @@ +#include +#include + +#ifndef SHARED_H +#define SHARED_H + +class Shared +{ +public: + Shared(); + static const QVector& shortcuts(); + static void addShortcut(QShortcut* shortcut, QString message = QString()); +}; + +#endif // SHARED_H diff --git a/znotes.pro b/znotes.pro index 9afc8a6..2a8133a 100644 --- a/znotes.pro +++ b/znotes.pro @@ -34,7 +34,8 @@ SOURCES += main.cpp \ single_inst/qtlocalpeer.cpp \ single_inst/qtsingleapplication.cpp \ ztabwidget.cpp \ - ztabbar.cpp + ztabbar.cpp \ + shared.cpp HEADERS += mainwindow.h \ configdialog.h \ settings.h \ @@ -58,7 +59,8 @@ HEADERS += mainwindow.h \ single_inst/qtlocalpeer.h \ single_inst/qtsingleapplication.h \ ztabwidget.h \ - ztabbar.h + ztabbar.h \ + shared.h FORMS += mainwindow.ui \ configdialog.ui \ aboutDialog.ui \