Browse Source

Editor: remember last used browse path when inserting image

Le Tan 7 years ago
parent
commit
a7ba7e9d58
3 changed files with 38 additions and 4 deletions
  1. 19 4
      src/dialog/vinsertimagedialog.cpp
  2. 5 0
      src/dialog/vinsertimagedialog.h
  3. 14 0
      src/vconfigmanager.h

+ 19 - 4
src/dialog/vinsertimagedialog.cpp

@@ -7,6 +7,9 @@
 #include "vmetawordlineedit.h"
 #include "vdownloader.h"
 #include "vlineedit.h"
+#include "vconfigmanager.h"
+
+extern VConfigManager *g_config;
 
 VInsertImageDialog::VInsertImageDialog(const QString &p_title,
                                        const QString &p_imageTitle,
@@ -166,15 +169,22 @@ QString VInsertImageDialog::getPathInput() const
 
 void VInsertImageDialog::handleBrowseBtnClicked()
 {
-    static QString lastPath = QDir::homePath();
+    QString bpath(m_browsePath);
+    if (bpath.isEmpty()) {
+        bpath = g_config->getImageBrowsePath();
+        if (bpath.isEmpty()) {
+            bpath = QDir::homePath();
+        }
+    }
+
     QString filePath = QFileDialog::getOpenFileName(this, tr("Select The Image To Be Inserted"),
-                                                    lastPath, tr("Images (*.png *.xpm *.jpg *.bmp *.gif *.svg)"));
+                                                    bpath, tr("Images (*.png *.xpm *.jpg *.bmp *.gif *.svg)"));
     if (filePath.isEmpty()) {
         return;
     }
 
-    // Update lastPath
-    lastPath = QFileInfo(filePath).path();
+    // Update browse path.
+    g_config->setImageBrowsePath(QFileInfo(filePath).path());
 
     m_imageType = ImageType::LocalFile;
 
@@ -360,3 +370,8 @@ void VInsertImageDialog::autoCompleteTitleFromPath()
     m_imageTitleEdit->setText(QFileInfo(imgPath).baseName());
     m_imageTitleEdit->selectAll();
 }
+
+void VInsertImageDialog::setBrowsePath(const QString &p_path)
+{
+    m_browsePath = p_path;
+}

+ 5 - 0
src/dialog/vinsertimagedialog.h

@@ -46,6 +46,8 @@ public:
     // Return 0 if no override.
     int getOverridenWidth() const;
 
+    void setBrowsePath(const QString &p_path);
+
 public slots:
     void imageDownloaded(const QByteArray &data);
 
@@ -90,6 +92,9 @@ private:
     ImageType m_imageType;
 
     QSharedPointer<QTemporaryFile> m_tempFile;
+
+    // Default path when browsing images to insert.
+    QString m_browsePath;
 };
 
 inline VInsertImageDialog::ImageType VInsertImageDialog::getImageType() const

+ 14 - 0
src/vconfigmanager.h

@@ -620,6 +620,10 @@ public:
     bool getEnableSplitTagFileList() const;
     void setEnableSplitTagFileList(bool p_enable);
 
+    // Get the path to browse when inserting image.
+    QString getImageBrowsePath() const;
+    void setImageBrowsePath(const QString &p_path);
+
 private:
     // Look up a config from user and default settings.
     QVariant getConfigFromSettings(const QString &section, const QString &key) const;
@@ -2840,4 +2844,14 @@ inline void VConfigManager::setEnableSplitTagFileList(bool p_enable)
 {
     setConfigToSettings("global", "split_tag_file_list", p_enable);
 }
+
+inline QString VConfigManager::getImageBrowsePath() const
+{
+    return getConfigFromSessionSettings("global", "image_browse_path").toString();
+}
+
+inline void VConfigManager::setImageBrowsePath(const QString &p_path)
+{
+    setConfigToSessionSettings("global", "image_browse_path", p_path);
+}
 #endif // VCONFIGMANAGER_H