Skip to content

Commit

Permalink
added automatic shortcut help generation
Browse files Browse the repository at this point in the history
  • Loading branch information
proton committed Jun 9, 2013
1 parent 522d5fa commit 17f089f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 53 deletions.
16 changes: 5 additions & 11 deletions aboutDialog.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -70,17 +71,10 @@ aboutDialog::aboutDialog(QWidget *parent) :
QString strHelp;
strHelp.append(QString("<p><b>%1</b><p>").arg(tr("Shortcuts:")));
strHelp.append(QString("<b>%1</b> - %2<br/>").arg("Alt+Fn").arg(tr("Go to note n")));
strHelp.append(QString("<b>%1</b> - %2<br/>").arg("Alt+Left").arg(tr("Go to previous note")));
strHelp.append(QString("<b>%1</b> - %2<br/>").arg("Alt+Right").arg(tr("Go to next note")));
strHelp.append(QString("<b>%1</b> - %2<br/>").arg("Ctrl+N").arg(tr("Create new note")));
strHelp.append(QString("<b>%1</b> - %2<br/>").arg("Ctrl+W").arg(tr("Remove current note")));
strHelp.append(QString("<b>%1</b> - %2<br/>").arg("F2").arg(tr("Rename current note")));
strHelp.append(QString("<b>%1</b> - %2<br/>").arg("Ctrl+F").arg(tr("Search in the notes' text")));
strHelp.append(QString("<b>%1</b> - %2<br/>").arg("Ctrl+B").arg(tr("Make selected text bold")));
strHelp.append(QString("<b>%1</b> - %2<br/>").arg("Ctrl+I").arg(tr("Make selected text italic")));
strHelp.append(QString("<b>%1</b> - %2<br/>").arg("Ctrl+S").arg(tr("Make selected text strikeout")));
strHelp.append(QString("<b>%1</b> - %2<br/>").arg("Ctrl+U").arg(tr("Make selected text underline")));
strHelp.append(QString("<b>%1</b> - %2<br/>").arg("Ctrl+Q").arg(tr("Exit program")));
QShortcut* shortcut;
foreach(shortcut, Shared::shortcuts())
if(!shortcut->whatsThis().isEmpty())
strHelp.append(QString("<b>%1</b> - %2<br/>").arg(shortcut->key().toString()).arg(shortcut->whatsThis()));
ui->lbHelp->setHtml(strHelp);
//adjustSize();
}
Expand Down
67 changes: 34 additions & 33 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "aboutDialog.h"
#include "note_html.h"
#include "notecreatewidget.h"
#include "shared.h"

#include <QMessageBox>
#include <QClipboard>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -324,6 +326,8 @@ inline QAction* GenerateAction(int item_id /*, bool checkable = false*/)
return action;
}

#include <QtDebug>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow), note_create_widget(0)
{
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 2 additions & 7 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
//
Expand Down
18 changes: 18 additions & 0 deletions shared.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "shared.h"

QVector<QShortcut*> p_shortcuts;

Shared::Shared()
{
}

const QVector<QShortcut*>& Shared::shortcuts()
{
return p_shortcuts;
}

void Shared::addShortcut(QShortcut* shortcut, QString comment)
{
shortcut->setWhatsThis(comment);
p_shortcuts << shortcut;
}
15 changes: 15 additions & 0 deletions shared.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <QVector>
#include <QShortcut>

#ifndef SHARED_H
#define SHARED_H

class Shared
{
public:
Shared();
static const QVector<QShortcut*>& shortcuts();
static void addShortcut(QShortcut* shortcut, QString message = QString());
};

#endif // SHARED_H
6 changes: 4 additions & 2 deletions znotes.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down

0 comments on commit 17f089f

Please sign in to comment.