diff --git a/src/dialog/vinsertimagedialog.cpp b/src/dialog/vinsertimagedialog.cpp index 06727c48..fc7d72b9 100644 --- a/src/dialog/vinsertimagedialog.cpp +++ b/src/dialog/vinsertimagedialog.cpp @@ -29,31 +29,33 @@ VInsertImageDialog::~VInsertImageDialog() void VInsertImageDialog::setupUI() { - pathLabel = new QLabel(tr("&From")); + pathLabel = new QLabel(tr("&From:")); pathEdit = new QLineEdit(defaultPath); pathLabel->setBuddy(pathEdit); browseBtn = new QPushButton(tr("&Browse")); QHBoxLayout *pathLayout = new QHBoxLayout(); + pathLayout->addWidget(pathLabel); pathLayout->addWidget(pathEdit); pathLayout->addWidget(browseBtn); - imageTitleLabel = new QLabel(tr("&Title")); + imageTitleLabel = new QLabel(tr("&Title:")); imageTitleEdit = new QLineEdit(defaultImageTitle); imageTitleEdit->selectAll(); imageTitleLabel->setBuddy(imageTitleEdit); QRegExp regExp("[\\w\\(\\)@#%\\*\\-\\+=\\?<>\\,\\.\\s]+"); QValidator *validator = new QRegExpValidator(regExp, this); imageTitleEdit->setValidator(validator); + QHBoxLayout *titleLayout = new QHBoxLayout(); + titleLayout->addWidget(imageTitleLabel); + titleLayout->addWidget(imageTitleEdit); okBtn = new QPushButton(tr("&OK")); okBtn->setDefault(true); cancelBtn = new QPushButton(tr("&Cancel")); QVBoxLayout *topLayout = new QVBoxLayout(); - topLayout->addWidget(pathLabel); topLayout->addLayout(pathLayout); - topLayout->addWidget(imageTitleLabel); - topLayout->addWidget(imageTitleEdit); + topLayout->addLayout(titleLayout); QHBoxLayout *btmLayout = new QHBoxLayout(); btmLayout->addStretch(); @@ -96,10 +98,19 @@ void VInsertImageDialog::handleBrowseBtnClicked() static QString lastPath = QDir::homePath(); QString filePath = QFileDialog::getOpenFileName(this, tr("Select the image to be inserted"), lastPath, tr("Images (*.png *.xpm *.jpg *.bmp *.gif)")); + if (filePath.isNull() || filePath.isEmpty()) { + return; + } + // Update lastPath lastPath = QFileInfo(filePath).path(); pathEdit->setText(filePath); + QImage image(filePath); + if (image.isNull()) { + return; + } + setImage(image); } void VInsertImageDialog::setImage(const QImage &image) diff --git a/src/resources/icons/import_note.svg b/src/resources/icons/import_note.svg new file mode 100644 index 00000000..8c9d75b9 --- /dev/null +++ b/src/resources/icons/import_note.svg @@ -0,0 +1,15 @@ + + + + + + + + diff --git a/src/resources/icons/insert_image.svg b/src/resources/icons/insert_image.svg new file mode 100644 index 00000000..b33d4ea6 --- /dev/null +++ b/src/resources/icons/insert_image.svg @@ -0,0 +1,13 @@ + + + + + + + + + diff --git a/src/vedit.cpp b/src/vedit.cpp index 865eddd8..4b329593 100644 --- a/src/vedit.cpp +++ b/src/vedit.cpp @@ -80,3 +80,10 @@ void VEdit::setModified(bool p_modified) m_file->setModified(p_modified); } } + +void VEdit::insertImage() +{ + if (m_editOps) { + m_editOps->insertImage(); + } +} diff --git a/src/vedit.h b/src/vedit.h index aa264063..3f5a7ff7 100644 --- a/src/vedit.h +++ b/src/vedit.h @@ -24,6 +24,8 @@ public: bool isModified() const; virtual void reloadFile(); virtual void scrollToLine(int p_lineNumber); + // User requests to insert an image. + virtual void insertImage(); protected: QPointer m_file; diff --git a/src/veditarea.cpp b/src/veditarea.cpp index 3f4f38ef..1f0c5d9a 100644 --- a/src/veditarea.cpp +++ b/src/veditarea.cpp @@ -150,7 +150,7 @@ void VEditArea::updateWindowStatus() { if (curWindowIndex == -1) { Q_ASSERT(splitter->count() == 0); - emit curTabStatusChanged(NULL, false); + emit curTabStatusChanged(NULL, NULL, false); emit outlineChanged(VToc()); emit curHeaderChanged(VAnchor()); return; @@ -376,3 +376,12 @@ void VEditArea::handleNotebookUpdated(const VNotebook *p_notebook) getWindow(i)->updateNotebookInfo(p_notebook); } } + +VEditTab *VEditArea::currentEditTab() +{ + if (curWindowIndex == -1) { + return NULL; + } + VEditWindow *win = getWindow(curWindowIndex); + return win->currentEditTab(); +} diff --git a/src/veditarea.h b/src/veditarea.h index 021d6eb1..49268ec6 100644 --- a/src/veditarea.h +++ b/src/veditarea.h @@ -28,9 +28,11 @@ public: bool closeFile(const VFile *p_file, bool p_forced); bool closeFile(const VDirectory *p_dir, bool p_forced); bool closeFile(const VNotebook *p_notebook, bool p_forced); + // Returns current edit tab. + VEditTab *currentEditTab(); signals: - void curTabStatusChanged(const VFile *p_file, bool p_editMode); + void curTabStatusChanged(const VFile *p_file, const VEditTab *p_editTab, bool p_editMode); void outlineChanged(const VToc &toc); void curHeaderChanged(const VAnchor &anchor); diff --git a/src/veditoperations.h b/src/veditoperations.h index e0136a39..a0b9e431 100644 --- a/src/veditoperations.h +++ b/src/veditoperations.h @@ -14,6 +14,7 @@ public: virtual ~VEditOperations(); virtual bool insertImageFromMimeData(const QMimeData *source) = 0; virtual bool insertURLFromMimeData(const QMimeData *source) = 0; + virtual bool insertImage() = 0; protected: void insertTextAtCurPos(const QString &text); diff --git a/src/vedittab.cpp b/src/vedittab.cpp index 81937b85..34953b5f 100644 --- a/src/vedittab.cpp +++ b/src/vedittab.cpp @@ -434,3 +434,12 @@ void VEditTab::updateCurHeader(int p_lineNumber, int p_outlineIndex) emit curHeaderChanged(curHeader); } } + +void VEditTab::insertImage() +{ + qDebug() << "insert image"; + if (!isEditMode) { + return; + } + m_textEditor->insertImage(); +} diff --git a/src/vedittab.h b/src/vedittab.h index 3cda35e6..a1129a48 100644 --- a/src/vedittab.h +++ b/src/vedittab.h @@ -37,6 +37,7 @@ public: void requestUpdateCurHeader(); void scrollToAnchor(const VAnchor& anchor); inline VFile *getFile(); + void insertImage(); signals: void getFocused(); diff --git a/src/veditwindow.cpp b/src/veditwindow.cpp index a1f0eaf2..94e5ec46 100644 --- a/src/veditwindow.cpp +++ b/src/veditwindow.cpp @@ -284,7 +284,7 @@ void VEditWindow::saveFile() void VEditWindow::noticeTabStatus(int p_index) { if (p_index == -1) { - emit tabStatusChanged(NULL, false); + emit tabStatusChanged(NULL, NULL, false); return; } @@ -296,7 +296,7 @@ void VEditWindow::noticeTabStatus(int p_index) QTabBar *tabs = tabBar(); tabs->setTabText(p_index, generateTabText(file->getName(), file->isModified())); tabs->setTabToolTip(p_index, generateTooltip(file)); - emit tabStatusChanged(file, editMode); + emit tabStatusChanged(file, editor, editMode); } // Be requested to report current status @@ -554,3 +554,12 @@ void VEditWindow::setLeftCornerWidgetVisible(bool p_visible) setCornerWidget(NULL, Qt::TopLeftCorner); } } + +VEditTab *VEditWindow::currentEditTab() +{ + int idx = currentIndex(); + if (idx == -1) { + return NULL; + } + return getTab(idx); +} diff --git a/src/veditwindow.h b/src/veditwindow.h index ccedfa32..a9a375f8 100644 --- a/src/veditwindow.h +++ b/src/veditwindow.h @@ -39,13 +39,14 @@ public: void updateFileInfo(const VFile *p_file); void updateDirectoryInfo(const VDirectory *p_dir); void updateNotebookInfo(const VNotebook *p_notebook); + VEditTab *currentEditTab(); protected: void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; bool eventFilter(QObject *watched, QEvent *event) Q_DECL_OVERRIDE; signals: - void tabStatusChanged(const VFile *p_file, bool p_editMode); + void tabStatusChanged(const VFile *p_file, const VEditTab *p_editTab, bool p_editMode); void requestSplitWindow(VEditWindow *curWindow); void requestRemoveSplit(VEditWindow *curWindow); // This widget or its children get the focus diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index 1a7e18de..5fc2d64b 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -149,7 +149,8 @@ void VMainWindow::initActions() initViewActions(); - importNoteAct = new QAction(tr("&Import note from file"), this); + importNoteAct = new QAction(QIcon(":/resources/icons/import_note.svg"), + tr("&Import Notes From Files"), this); importNoteAct->setStatusTip(tr("Import notes into current directory from files")); connect(importNoteAct, &QAction::triggered, this, &VMainWindow::importNoteFromFile); @@ -175,22 +176,28 @@ void VMainWindow::initActions() connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt); - expandTabAct = new QAction(tr("&Expand tab"), this); + m_insertImageAct = new QAction(QIcon(":/resources/icons/insert_image.svg"), + tr("Insert &Image"), this); + m_insertImageAct->setStatusTip(tr("Insert an image from file")); + connect(m_insertImageAct, &QAction::triggered, + this, &VMainWindow::insertImage); + + expandTabAct = new QAction(tr("&Expand Tab"), this); expandTabAct->setStatusTip(tr("Expand tab to spaces")); expandTabAct->setCheckable(true); connect(expandTabAct, &QAction::triggered, this, &VMainWindow::changeExpandTab); tabStopWidthAct = new QActionGroup(this); - twoSpaceTabAct = new QAction(tr("2 spaces"), tabStopWidthAct); + twoSpaceTabAct = new QAction(tr("2 Spaces"), tabStopWidthAct); twoSpaceTabAct->setStatusTip(tr("Expand tab to 2 spaces")); twoSpaceTabAct->setCheckable(true); twoSpaceTabAct->setData(2); - fourSpaceTabAct = new QAction(tr("4 spaces"), tabStopWidthAct); + fourSpaceTabAct = new QAction(tr("4 Spaces"), tabStopWidthAct); fourSpaceTabAct->setStatusTip(tr("Expand tab to 4 spaces")); fourSpaceTabAct->setCheckable(true); fourSpaceTabAct->setData(4); - eightSpaceTabAct = new QAction(tr("8 spaces"), tabStopWidthAct); + eightSpaceTabAct = new QAction(tr("8 Spaces"), tabStopWidthAct); eightSpaceTabAct->setStatusTip(tr("Expand tab to 8 spaces")); eightSpaceTabAct->setCheckable(true); eightSpaceTabAct->setData(8); @@ -308,6 +315,8 @@ void VMainWindow::initMenuBar() fileMenu->addAction(importNoteAct); // Edit Menu + editMenu->addAction(m_insertImageAct); + editMenu->addSeparator(); editMenu->addAction(expandTabAct); if (vconfig.getIsExpandTab()) { expandTabAct->setChecked(true); @@ -554,7 +563,7 @@ void VMainWindow::updateToolbarFromTabChage(const VFile *p_file, bool p_editMode } } -void VMainWindow::handleCurTabStatusChanged(const VFile *p_file, bool p_editMode) +void VMainWindow::handleCurTabStatusChanged(const VFile *p_file, const VEditTab *p_editTab, bool p_editMode) { updateToolbarFromTabChage(p_file, p_editMode); @@ -567,6 +576,7 @@ void VMainWindow::handleCurTabStatusChanged(const VFile *p_file, bool p_editMode } updateWindowTitle(title); m_curFile = const_cast(p_file); + m_curTab = const_cast(p_editTab); } void VMainWindow::onePanelView() @@ -701,3 +711,12 @@ void VMainWindow::repositionAvatar() m_avatar->move(x, y); m_avatar->show(); } + +void VMainWindow::insertImage() +{ + if (!m_curTab) { + return; + } + Q_ASSERT(m_curTab == editArea->currentEditTab()); + m_curTab->insertImage(); +} diff --git a/src/vmainwindow.h b/src/vmainwindow.h index fc4b3e38..0d23ad48 100644 --- a/src/vmainwindow.h +++ b/src/vmainwindow.h @@ -7,6 +7,7 @@ #include #include #include "vfile.h" +#include "vedittab.h" class QLabel; class QComboBox; @@ -43,7 +44,7 @@ private slots: void setTabStopWidth(QAction *action); void setEditorBackgroundColor(QAction *action); void setRenderBackgroundColor(QAction *action); - void handleCurTabStatusChanged(const VFile *p_file, bool p_editMode); + void handleCurTabStatusChanged(const VFile *p_file, const VEditTab *p_editTab, bool p_editMode); void onePanelView(); void twoPanelView(); void expandPanelView(bool p_checked); @@ -51,6 +52,7 @@ private slots: void deleteCurNote(); void handleCurrentDirectoryChanged(const VDirectory *p_dir); void handleCurrentNotebookChanged(const VNotebook *p_notebook); + void insertImage(); protected: void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE; @@ -78,6 +80,7 @@ private: VNote *vnote; QPointer m_curFile; + QPointer m_curTab; QLabel *notebookLabel; QLabel *directoryLabel; @@ -111,6 +114,7 @@ private: QAction *hoedownAct; QAction *aboutAct; QAction *aboutQtAct; + QAction *m_insertImageAct; QAction *expandTabAct; QActionGroup *tabStopWidthAct; QAction *twoSpaceTabAct; diff --git a/src/vmdedit.cpp b/src/vmdedit.cpp index 287ae093..d4e967b2 100644 --- a/src/vmdedit.cpp +++ b/src/vmdedit.cpp @@ -120,7 +120,7 @@ void VMdEdit::insertFromMimeData(const QMimeData *source) VEdit::insertFromMimeData(source); } -void VMdEdit::insertImage(const QString &p_name) +void VMdEdit::imageInserted(const QString &p_name) { m_insertedImages.append(p_name); } diff --git a/src/vmdedit.h b/src/vmdedit.h index 945b54aa..79ba6b1c 100644 --- a/src/vmdedit.h +++ b/src/vmdedit.h @@ -18,7 +18,8 @@ public: void saveFile() Q_DECL_OVERRIDE; void reloadFile() Q_DECL_OVERRIDE; - void insertImage(const QString &p_name); + // An image has been inserted. + void imageInserted(const QString &p_name); // Scroll to m_headers[p_headerIndex]. void scrollToHeader(int p_headerIndex); diff --git a/src/vmdeditoperations.cpp b/src/vmdeditoperations.cpp index da877df5..9b27fbe4 100644 --- a/src/vmdeditoperations.cpp +++ b/src/vmdeditoperations.cpp @@ -57,7 +57,7 @@ void VMdEditOperations::insertImageFromQImage(const QString &title, const QStrin VMdEdit *mdEditor = dynamic_cast(m_editor); Q_ASSERT(mdEditor); - mdEditor->insertImage(fileName); + mdEditor->imageInserted(fileName); } void VMdEditOperations::insertImageFromPath(const QString &title, @@ -82,7 +82,7 @@ void VMdEditOperations::insertImageFromPath(const QString &title, VMdEdit *mdEditor = dynamic_cast(m_editor); Q_ASSERT(mdEditor); - mdEditor->insertImage(fileName); + mdEditor->imageInserted(fileName); } bool VMdEditOperations::insertImageFromURL(const QUrl &imageUrl) @@ -149,3 +149,16 @@ bool VMdEditOperations::insertURLFromMimeData(const QMimeData *source) } return true; } + +bool VMdEditOperations::insertImage() +{ + VInsertImageDialog dialog(QObject::tr("Insert Image From File"), QObject::tr("image_title"), + "", (QWidget *)m_editor); + if (dialog.exec() == QDialog::Accepted) { + QString title = dialog.getImageTitleInput(); + QString imagePath = dialog.getPathInput(); + qDebug() << "insert image from" << imagePath << "as" << title; + insertImageFromPath(title, m_file->retriveImagePath(), imagePath); + } + return true; +} diff --git a/src/vmdeditoperations.h b/src/vmdeditoperations.h index 4ef8f55d..222d1dbd 100644 --- a/src/vmdeditoperations.h +++ b/src/vmdeditoperations.h @@ -14,6 +14,9 @@ public: VMdEditOperations(VEdit *p_editor, VFile *p_file); bool insertImageFromMimeData(const QMimeData *source) Q_DECL_OVERRIDE; bool insertURLFromMimeData(const QMimeData *source) Q_DECL_OVERRIDE; + bool insertImage() Q_DECL_OVERRIDE; + +private: bool insertImageFromURL(const QUrl &imageUrl); void insertImageFromPath(const QString &title, const QString &path, const QString &oriImagePath); void insertImageFromQImage(const QString &title, const QString &path, const QImage &image); diff --git a/src/vnote.qrc b/src/vnote.qrc index 580ad0af..6ab9a756 100644 --- a/src/vnote.qrc +++ b/src/vnote.qrc @@ -72,5 +72,7 @@ resources/icons/arrow_dropdown.svg resources/icons/current_tab.svg resources/icons/vnote.png + resources/icons/insert_image.svg + resources/icons/import_note.svg