Browse Source

AttachmentList: support Info action to rename an attachment

This will fix the input method issue on macOS.
Le Tan 7 years ago
parent
commit
c302cd3bfc
5 changed files with 119 additions and 3 deletions
  1. 36 0
      src/utils/vutils.cpp
  2. 8 0
      src/utils/vutils.h
  3. 65 3
      src/vattachmentlist.cpp
  4. 4 0
      src/vattachmentlist.h
  5. 6 0
      src/vexplorer.cpp

+ 36 - 0
src/utils/vutils.cpp

@@ -1581,6 +1581,42 @@ QString VUtils::promptForFileName(const QString &p_title,
     return name;
 }
 
+QString VUtils::promptForFileName(const QString &p_title,
+                                  const QString &p_label,
+                                  const QString &p_default,
+                                  std::function<bool(const QString &p_name)> p_checkExistsFunc,
+                                  QWidget *p_parent)
+{
+    QString name = p_default;
+    QString text = p_label;
+    while (true) {
+        bool ok;
+        name = QInputDialog::getText(p_parent,
+                                     p_title,
+                                     text,
+                                     QLineEdit::Normal,
+                                     name,
+                                     &ok);
+        if (!ok || name.isEmpty()) {
+            return "";
+        }
+
+        if (!VUtils::checkFileNameLegal(name)) {
+            text = QObject::tr("Illegal name. Please try again:");
+            continue;
+        }
+
+        if (p_checkExistsFunc(name)) {
+            text = QObject::tr("Name already exists. Please try again:");
+            continue;
+        }
+
+        break;
+    }
+
+    return name;
+}
+
 bool VUtils::onlyHasImgInHtml(const QString &p_html)
 {
     // Tricky.

+ 8 - 0
src/utils/vutils.h

@@ -8,6 +8,8 @@
 #include <QMessageBox>
 #include <QUrl>
 #include <QDir>
+#include <functional>
+
 #include "vconfigmanager.h"
 #include "vconstants.h"
 
@@ -339,6 +341,12 @@ public:
                                      const QString &p_dir,
                                      QWidget *p_parent = nullptr);
 
+    static QString promptForFileName(const QString &p_title,
+                                     const QString &p_label,
+                                     const QString &p_default,
+                                     std::function<bool(const QString &p_name)> p_checkExistsFunc,
+                                     QWidget *p_parent = nullptr);
+
     // Whether @p_html has only <img> content.
     static bool onlyHasImgInHtml(const QString &p_html);
 

+ 65 - 3
src/vattachmentlist.cpp

@@ -13,8 +13,11 @@
 #include "vlineedit.h"
 
 extern VConfigManager *g_config;
+
 extern VMainWindow *g_mainWin;
 
+const QString VAttachmentList::c_infoShortcutSequence = "F2";
+
 VAttachmentList::VAttachmentList(QWidget *p_parent)
     : QWidget(p_parent),
       VButtonPopupWidget(this),
@@ -239,12 +242,13 @@ void VAttachmentList::handleContextMenuRequested(QPoint p_pos)
         return;
     }
 
+    int selectedSize = m_attachmentList->selectedItems().size();
     if (item) {
         if (!item->isSelected()) {
             m_attachmentList->setCurrentItem(item, QItemSelectionModel::ClearAndSelect);
         }
 
-        if (m_attachmentList->selectedItems().size() == 1) {
+        if (selectedSize == 1) {
             QAction *openAct = new QAction(tr("&Open"), &menu);
             openAct->setToolTip(tr("Open current attachment file"));
             connect(openAct, &QAction::triggered,
@@ -280,6 +284,18 @@ void VAttachmentList::handleContextMenuRequested(QPoint p_pos)
         menu.addAction(sortAct);
     }
 
+    if (selectedSize == 1) {
+        menu.addSeparator();
+
+        QAction *fileInfoAct = new QAction(VIconUtils::menuIcon(":/resources/icons/note_info.svg"),
+                                           tr("&Info\t%1").arg(VUtils::getShortcutText(c_infoShortcutSequence)),
+                                           &menu);
+        fileInfoAct->setToolTip(tr("View and edit current folder's information"));
+        connect(fileInfoAct, &QAction::triggered,
+                this, &VAttachmentList::attachmentInfo);
+        menu.addAction(fileInfoAct);
+    }
+
     if (!menu.actions().isEmpty()) {
         menu.exec(mapToGlobal(p_pos));
     }
@@ -439,7 +455,7 @@ void VAttachmentList::handleListItemCommitData(QWidget *p_itemEdit)
         item->setText(oldText);
     } else {
         if (!m_file->renameAttachment(oldText, text)) {
-            VUtils::showMessage(QMessageBox::Information,
+            VUtils::showMessage(QMessageBox::Warning,
                                 tr("Rename Attachment"),
                                 tr("Fail to rename attachment <span style=\"%1\">%2</span>.")
                                   .arg(g_config->c_dataTextStyle)
@@ -447,7 +463,7 @@ void VAttachmentList::handleListItemCommitData(QWidget *p_itemEdit)
                                 "",
                                 QMessageBox::Ok,
                                 QMessageBox::Ok,
-                                this);
+                                g_mainWin);
             // Recover to old name.
             item->setText(oldText);
         } else {
@@ -634,5 +650,51 @@ void VAttachmentList::init()
 
     setupUI();
 
+    QShortcut *infoShortcut = new QShortcut(QKeySequence(c_infoShortcutSequence), this);
+    infoShortcut->setContext(Qt::WidgetWithChildrenShortcut);
+    connect(infoShortcut, &QShortcut::activated,
+            this, &VAttachmentList::attachmentInfo);
+
     updateContent();
 }
+
+void VAttachmentList::attachmentInfo()
+{
+    QListWidgetItem *item = m_attachmentList->currentItem();
+    if (!item) {
+        return;
+    }
+
+    QString oldName = item->data(Qt::UserRole).toString();
+    QString name = VUtils::promptForFileName(tr("Attachment Information"),
+                                             tr("Rename attachment (%1):").arg(oldName),
+                                             oldName,
+                                             [this](const QString &p_name) {
+                                                if (m_file->findAttachment(p_name, false) > -1) {
+                                                    return true;
+                                                }
+
+                                                return false;
+                                             },
+                                             g_mainWin);
+
+    if (name.isEmpty()) {
+        return;
+    }
+
+    if (!m_file->renameAttachment(oldName, name)) {
+        VUtils::showMessage(QMessageBox::Warning,
+                            tr("Attachment Information"),
+                            tr("Fail to rename attachment <span style=\"%1\">%2</span>.")
+                              .arg(g_config->c_dataTextStyle)
+                              .arg(oldName),
+                            "",
+                            QMessageBox::Ok,
+                            QMessageBox::Ok,
+                            g_mainWin);
+    } else {
+        // Change the data.
+        item->setData(Qt::UserRole, name);
+        item->setText(name);
+    }
+}

+ 4 - 0
src/vattachmentlist.h

@@ -50,6 +50,8 @@ private slots:
 
     void handleListItemCommitData(QWidget *p_itemEdit);
 
+    void attachmentInfo();
+
 private:
     void setupUI();
 
@@ -79,6 +81,8 @@ private:
     QListWidget *m_attachmentList;
 
     VNoteFile *m_file;
+
+    static const QString c_infoShortcutSequence;
 };
 
 #endif // VATTACHMENTLIST_H

+ 6 - 0
src/vexplorer.cpp

@@ -236,6 +236,12 @@ void VExplorer::init()
     infoShortcut->setContext(Qt::WidgetWithChildrenShortcut);
     connect(infoShortcut, &QShortcut::activated,
             this, [this]() {
+                QModelIndexList selectedIdx = m_tree->selectionModel()->selectedRows();
+                if (selectedIdx.size() == 1) {
+                    QFileSystemModel *model = static_cast<QFileSystemModel *>(m_tree->model());
+                    QString filePath = model->filePath(selectedIdx[0]);
+                    renameFile(filePath);
+                }
             });
 
     g_config->getExplorerEntries(m_entries);