Browse Source

implement insert image menu action

Signed-off-by: Le Tan <[email protected]>
Le Tan 9 years ago
parent
commit
5ff46d6237

+ 16 - 5
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)

+ 15 - 0
src/resources/icons/import_note.svg

@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 16.2.1, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
+	 width="512px" height="512px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
+<g>
+	<path d="M398.6,169.2c-0.9-2.2-2-4.3-3.5-6.1l-83.8-91.7c-1.9-2.1-4.2-3.6-6.7-4.9c-2.9-1.5-6.1-2.1-9.5-2.1H135.2
+		c-12.4,0-22.7,10.6-22.7,23.9v335.2c0,13.4,10.3,24.9,22.7,24.9h243.1c12.4,0,22.2-11.5,22.2-24.9V179.4
+		C400.5,175.8,400,172.3,398.6,169.2z M160.5,178.6c0-1.5,1.8-2.1,3.4-2.1h70.8c1.6,0,2.8,0.6,2.8,2.1v10.8c0,1.4-1.1,3.1-2.8,3.1
+		h-70.8c-1.6,0-3.4-1.7-3.4-3.1V178.6z M160.5,306.6c0-1.5,1.8-2.1,3.4-2.1h122.2c1.6,0,2.4,0.6,2.4,2.1v10.8c0,1.4-0.7,3.1-2.4,3.1
+		H163.9c-1.6,0-3.4-1.7-3.4-3.1V306.6z M320.5,381.4c0,1.4-0.7,3.1-2.4,3.1H163.9c-1.6,0-3.4-1.7-3.4-3.1v-10.8
+		c0-1.5,1.8-2.1,3.4-2.1h154.2c1.6,0,2.4,0.6,2.4,2.1V381.4z M352.5,253.4c0,1.4-0.7,3.1-2.4,3.1H163.9c-1.6,0-3.4-1.7-3.4-3.1
+		v-10.8c0-1.5,1.8-2.1,3.4-2.1h186.2c1.6,0,2.4,0.6,2.4,2.1V253.4z M305.6,177.5c-5.6,0-11.1-5.2-11.1-11.3v-66l71.2,77.3H305.6z"/>
+</g>
+</svg>

+ 13 - 0
src/resources/icons/insert_image.svg

@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 16.2.1, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
+	 width="512px" height="512px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
+<g>
+	<path d="M368,224c26.5,0,48-21.5,48-48c0-26.5-21.5-48-48-48c-26.5,0-48,21.5-48,48C320,202.5,341.5,224,368,224z"/>
+	<path d="M452,64H60c-15.6,0-28,12.7-28,28.3v327.4c0,15.6,12.4,28.3,28,28.3h392c15.6,0,28-12.7,28-28.3V92.3
+		C480,76.7,467.6,64,452,64z M348.9,261.7c-3-3.5-7.6-6.2-12.8-6.2c-5.1,0-8.7,2.4-12.8,5.7l-18.7,15.8c-3.9,2.8-7,4.7-11.5,4.7
+		c-4.3,0-8.2-1.6-11-4.1c-1-0.9-2.8-2.6-4.3-4.1L224,215.3c-4-4.6-10-7.5-16.7-7.5c-6.7,0-12.9,3.3-16.8,7.8L64,368.2V107.7
+		c1-6.8,6.3-11.7,13.1-11.7h357.7c6.9,0,12.5,5.1,12.9,12l0.3,260.4L348.9,261.7z"/>
+</g>
+</svg>

+ 7 - 0
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();
+    }
+}

+ 2 - 0
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<VFile> m_file;

+ 10 - 1
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();
+}

+ 3 - 1
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);
 

+ 1 - 0
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);

+ 9 - 0
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();
+}

+ 1 - 0
src/vedittab.h

@@ -37,6 +37,7 @@ public:
     void requestUpdateCurHeader();
     void scrollToAnchor(const VAnchor& anchor);
     inline VFile *getFile();
+    void insertImage();
 
 signals:
     void getFocused();

+ 11 - 2
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);
+}

+ 2 - 1
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

+ 25 - 6
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<VFile *>(p_file);
+    m_curTab = const_cast<VEditTab *>(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();
+}

+ 5 - 1
src/vmainwindow.h

@@ -7,6 +7,7 @@
 #include <QPointer>
 #include <QString>
 #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<VFile> m_curFile;
+    QPointer<VEditTab> 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;

+ 1 - 1
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);
 }

+ 2 - 1
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);

+ 15 - 2
src/vmdeditoperations.cpp

@@ -57,7 +57,7 @@ void VMdEditOperations::insertImageFromQImage(const QString &title, const QStrin
 
     VMdEdit *mdEditor = dynamic_cast<VMdEdit *>(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<VMdEdit *>(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;
+}

+ 3 - 0
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);

+ 2 - 0
src/vnote.qrc

@@ -72,5 +72,7 @@
         <file>resources/icons/arrow_dropdown.svg</file>
         <file>resources/icons/current_tab.svg</file>
         <file>resources/icons/vnote.png</file>
+        <file>resources/icons/insert_image.svg</file>
+        <file>resources/icons/import_note.svg</file>
     </qresource>
 </RCC>