add menus and toolbars

1. Add menu item "Converter" to choose markdown converter;
2. Add menu item "About" and "About Qt";
3. Change all QMessageBox's parent to "this";

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-10-23 12:01:31 +08:00
parent f06be44496
commit b85bf8bdc7
6 changed files with 98 additions and 20 deletions

View File

@ -57,6 +57,7 @@ public:
inline hoedown_extensions getMarkdownExtensions() const;
inline MarkdownConverterType getMdConverterType() const;
inline void setMarkdownConverterType(MarkdownConverterType type);
inline QString getPreTemplatePath() const;
inline QString getPostTemplatePath() const;
@ -176,4 +177,13 @@ inline QString VConfigManager::getPostTemplatePath() const
return postTemplatePath;
}
inline void VConfigManager::setMarkdownConverterType(MarkdownConverterType type)
{
if (mdConverterType == type) {
return;
}
mdConverterType = type;
setConfigToSettings("global", "markdown_converter", type);
}
#endif // VCONFIGMANAGER_H

View File

@ -136,7 +136,8 @@ void VDirectoryTree::updateDirectoryTreeTopLevel()
if (!validatePath(path)) {
qDebug() << "invalid notebook path:" << path;
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook path."));
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook path."),
QMessageBox::Ok, this);
msgBox.setInformativeText(QString("Notebook path \"%1\" either does not exist or is not valid.")
.arg(path));
msgBox.exec();
@ -146,7 +147,8 @@ void VDirectoryTree::updateDirectoryTreeTopLevel()
QJsonObject configJson = VConfigManager::readDirectoryConfig(path);
if (configJson.isEmpty()) {
qDebug() << "invalid notebook configuration for path:" << path;
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook configuration."));
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook configuration."),
QMessageBox::Ok, this);
msgBox.setInformativeText(QString("Notebook path \"%1\" does not contain a valid configuration file.")
.arg(path));
msgBox.exec();
@ -177,7 +179,8 @@ void VDirectoryTree::updateDirectoryTreeOne(QTreeWidgetItem &parent, int depth)
QString path(QDir::cleanPath(treePath + QDir::separator() + relativePath));
if (!validatePath(path)) {
qDebug() << "invalide notebook directory:" << path;
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook directory."));
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook directory."),
QMessageBox::Ok, this);
msgBox.setInformativeText(QString("Notebook directory \"%1\" either does not exist or is not a valid notebook directory.")
.arg(path));
msgBox.exec();
@ -187,7 +190,8 @@ void VDirectoryTree::updateDirectoryTreeOne(QTreeWidgetItem &parent, int depth)
QJsonObject configJson = VConfigManager::readDirectoryConfig(path);
if (configJson.isEmpty()) {
qDebug() << "invalid notebook configuration for directory:" << path;
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook directory configuration."));
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook directory configuration."),
QMessageBox::Ok, this);
msgBox.setInformativeText(QString("Notebook directory \"%1\" does not contain a valid configuration file.")
.arg(path));
msgBox.exec();
@ -343,9 +347,8 @@ void VDirectoryTree::deleteDirectory()
QString curItemName = curItemJson["name"].toString();
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), QString("Are you sure you want to delete directory \"%1\"?")
.arg(curItemName));
.arg(curItemName), QMessageBox::Ok | QMessageBox::Cancel, this);
msgBox.setInformativeText(tr("This will delete any files under this directory."));
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Ok);
if (msgBox.exec() == QMessageBox::Ok) {
deleteDirectoryAndUpdateTree(curItem);
@ -366,7 +369,7 @@ QTreeWidgetItem* VDirectoryTree::createDirectoryAndUpdateTree(QTreeWidgetItem *p
if (!dir.mkdir(name)) {
qWarning() << "error: fail to create directory" << name << "under" << path;
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), QString("Could not create directory \"%1\" under \"%2\".")
.arg(name).arg(path));
.arg(name).arg(path), QMessageBox::Ok, this);
msgBox.setInformativeText(QString("Please check if there already exists a directory named \"%1\".").arg(name));
msgBox.exec();
return NULL;

View File

@ -139,7 +139,7 @@ void VEditor::readFile()
if (textEditor->isModified()) {
// Need to save the changes
QMessageBox msgBox;
QMessageBox msgBox(this);
msgBox.setText("The note has been modified.");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setIcon(QMessageBox::Information);
@ -175,8 +175,8 @@ bool VEditor::saveFile()
noteFile->content);
if (!ret) {
QMessageBox msgBox(QMessageBox::Warning, tr("Fail to save to file"),
QString("Fail to write to disk when saving a note. Please try it again."));
msgBox.setStandardButtons(QMessageBox::Ok);
QString("Fail to write to disk when saving a note. Please try it again."),
QMessageBox::Ok, this);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
textEditor->setModified(true);

View File

@ -58,7 +58,8 @@ void VFileList::updateFileList()
if (!QDir(path).exists()) {
qDebug() << "invalid notebook directory:" << path;
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook directory."));
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook directory."),
QMessageBox::Ok, this);
msgBox.setInformativeText(QString("Notebook directory \"%1\" either does not exist or is not valid.")
.arg(path));
msgBox.exec();
@ -68,7 +69,8 @@ void VFileList::updateFileList()
QJsonObject configJson = VConfigManager::readDirectoryConfig(path);
if (configJson.isEmpty()) {
qDebug() << "invalid notebook configuration for directory:" << path;
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook directory configuration."));
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook directory configuration."),
QMessageBox::Ok, this);
msgBox.setInformativeText(QString("Notebook directory \"%1\" does not contain a valid configuration file.")
.arg(path));
msgBox.exec();
@ -156,9 +158,9 @@ void VFileList::deleteFile()
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"),
QString("Are you sure you want to delete note \"%1\"?")
.arg(curItemName));
.arg(curItemName), QMessageBox::Ok | QMessageBox::Cancel,
this);
msgBox.setInformativeText(tr("This may be not recoverable."));
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Ok);
if (msgBox.exec() == QMessageBox::Ok) {
// First close this file forcely
@ -209,7 +211,7 @@ QListWidgetItem* VFileList::createFileAndUpdateList(const QString &name,
if (!file.open(QIODevice::WriteOnly)) {
qWarning() << "error: fail to create file:" << filePath;
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), QString("Could not create file \"%1\" under \"%2\".")
.arg(name).arg(path));
.arg(name).arg(path), QMessageBox::Ok, this);
msgBox.setInformativeText(QString("Please check if there already exists a file named \"%1\".").arg(name));
msgBox.exec();
return NULL;

View File

@ -122,6 +122,27 @@ void VMainWindow::initActions()
importNoteAct->setStatusTip(tr("Import notes into current directory from files"));
connect(importNoteAct, &QAction::triggered,
this, &VMainWindow::importNoteFromFile);
converterAct = new QActionGroup(this);
markedAct = new QAction(tr("Marked"), converterAct);
markedAct->setStatusTip(tr("Use Marked to convert Markdown to HTML (Re-open current tabs to make it work)"));
markedAct->setCheckable(true);
markedAct->setData(int(MarkdownConverterType::Marked));
hoedownAct = new QAction(tr("Hoedown"), converterAct);
hoedownAct->setStatusTip(tr("Use Hoedown to convert Markdown to HTML (Re-open current tabs to make it work)"));
hoedownAct->setCheckable(true);
hoedownAct->setData(int(MarkdownConverterType::Hoedown));
connect(converterAct, &QActionGroup::triggered,
this, &VMainWindow::changeMarkdownConverter);
aboutAct = new QAction(tr("&About"), this);
aboutAct->setStatusTip(tr("Show information about VNote"));
connect(aboutAct, &QAction::triggered,
this, &VMainWindow::aboutMessage);
aboutQtAct = new QAction(tr("About &Qt"), this);
aboutQtAct->setStatusTip(tr("Show information about Qt"));
connect(aboutQtAct, &QAction::triggered,
qApp, &QApplication::aboutQt);
}
void VMainWindow::initToolBar()
@ -136,10 +157,28 @@ void VMainWindow::initToolBar()
void VMainWindow::initMenuBar()
{
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
QMenu *markdownMenu = menuBar()->addMenu(tr("&Markdown"));
QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
// To be implemented
// File Menu
fileMenu->addAction(importNoteAct);
// Markdown Menu
QMenu *converterMenu = markdownMenu->addMenu(tr("&Converter"));
converterMenu->addAction(hoedownAct);
converterMenu->addAction(markedAct);
MarkdownConverterType converterType = vconfig.getMdConverterType();
if (converterType == MarkdownConverterType::Marked) {
markedAct->setChecked(true);
} else if (converterType == MarkdownConverterType::Hoedown) {
hoedownAct->setChecked(true);
}
// Help menu
helpMenu->addAction(aboutQtAct);
helpMenu->addAction(aboutAct);
}
void VMainWindow::updateNotebookComboBox(const QVector<VNotebook> &notebooks)
@ -215,9 +254,8 @@ void VMainWindow::onDeleteNotebookBtnClicked()
QString curPath = vnote->getNotebooks()[curIndex].getPath();
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), QString("Are you sure you want to delete notebook \"%1\"?")
.arg(curName));
.arg(curName), QMessageBox::Ok | QMessageBox::Cancel, this);
msgBox.setInformativeText(QString("This will delete any files in this notebook (\"%1\").").arg(curPath));
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Ok);
if (msgBox.exec() == QMessageBox::Ok) {
vnote->removeNotebook(curName);
@ -240,10 +278,27 @@ void VMainWindow::importNoteFromFile()
}
QMessageBox msgBox(QMessageBox::Information, tr("Import note from file"),
QString("Imported notes: %1 succeed, %2 failed.")
.arg(files.size() - failedFiles.size()).arg(failedFiles.size()));
.arg(files.size() - failedFiles.size()).arg(failedFiles.size()),
QMessageBox::Ok, this);
if (!failedFiles.isEmpty()) {
msgBox.setInformativeText(tr("Failed to import files may be due to name conflicts."));
}
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec();
}
void VMainWindow::changeMarkdownConverter(QAction *action)
{
if (!action) {
return;
}
MarkdownConverterType type = (MarkdownConverterType)action->data().toInt();
qDebug() << "switch to converter" << type;
vconfig.setMarkdownConverterType(type);
}
void VMainWindow::aboutMessage()
{
QMessageBox::about(this, tr("About VNote"),
tr("VNote is a Vim-inspired note taking application for Markdown.\n"
"Visit https://github.com/tamlok/vnote.git for more information."));
}

View File

@ -16,6 +16,7 @@ class VTabWidget;
class QAction;
class QPushButton;
class VNotebook;
class QActionGroup;
class VMainWindow : public QMainWindow
{
@ -33,6 +34,8 @@ private slots:
void onDeleteNotebookBtnClicked();
void updateNotebookComboBox(const QVector<VNotebook> &notebooks);
void importNoteFromFile();
void changeMarkdownConverter(QAction *action);
void aboutMessage();
signals:
void curNotebookIndexChanged(const QString &path);
@ -61,6 +64,11 @@ private:
QAction *saveNoteAct;
QAction *readNoteAct;
QAction *importNoteAct;
QActionGroup *converterAct;
QAction *markedAct;
QAction *hoedownAct;
QAction *aboutAct;
QAction *aboutQtAct;
};
#endif // VMAINWINDOW_H