Browse Source

Modify tree context menu

Kandrashin Denis 13 years ago
parent
commit
86f47cb5c3
5 changed files with 72 additions and 17 deletions
  1. 3 0
      source/fb2main.cpp
  2. 39 10
      source/fb2tree.cpp
  3. 12 1
      source/fb2tree.hpp
  4. BIN
      source/ts/ru.qm
  5. 18 6
      source/ts/ru.ts

+ 3 - 0
source/fb2main.cpp

@@ -270,6 +270,7 @@ void Fb2MainWindow::createActions()
     menu->addSeparator();
     menu->addSeparator();
 
 
     actionCut = act = new QAction(FB2::icon("edit-cut"), tr("Cu&t"), this);
     actionCut = act = new QAction(FB2::icon("edit-cut"), tr("Cu&t"), this);
+    act->setShortcutContext(Qt::WidgetShortcut);
     act->setPriority(QAction::LowPriority);
     act->setPriority(QAction::LowPriority);
     act->setShortcuts(QKeySequence::Cut);
     act->setShortcuts(QKeySequence::Cut);
     act->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
     act->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
@@ -277,6 +278,7 @@ void Fb2MainWindow::createActions()
     menu->addAction(act);
     menu->addAction(act);
 
 
     actionCopy = act = new QAction(FB2::icon("edit-copy"), tr("&Copy"), this);
     actionCopy = act = new QAction(FB2::icon("edit-copy"), tr("&Copy"), this);
+    act->setShortcutContext(Qt::WidgetShortcut);
     act->setPriority(QAction::LowPriority);
     act->setPriority(QAction::LowPriority);
     act->setShortcuts(QKeySequence::Copy);
     act->setShortcuts(QKeySequence::Copy);
     act->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
     act->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
@@ -284,6 +286,7 @@ void Fb2MainWindow::createActions()
     menu->addAction(act);
     menu->addAction(act);
 
 
     actionPaste = act = new QAction(FB2::icon("edit-paste"), tr("&Paste"), this);
     actionPaste = act = new QAction(FB2::icon("edit-paste"), tr("&Paste"), this);
+    act->setShortcutContext(Qt::WidgetShortcut);
     act->setPriority(QAction::LowPriority);
     act->setPriority(QAction::LowPriority);
     act->setShortcuts(QKeySequence::Paste);
     act->setShortcuts(QKeySequence::Paste);
     act->setStatusTip(tr("Paste the clipboard's contents into the current selection"));
     act->setStatusTip(tr("Paste the clipboard's contents into the current selection"));

+ 39 - 10
source/fb2tree.cpp

@@ -3,6 +3,7 @@
 #include <QtDebug>
 #include <QtDebug>
 #include <QAction>
 #include <QAction>
 #include <QApplication>
 #include <QApplication>
+#include <QCursor>
 #include <QVBoxLayout>
 #include <QVBoxLayout>
 #include <QWebFrame>
 #include <QWebFrame>
 #include <QWebPage>
 #include <QWebPage>
@@ -331,12 +332,13 @@ Fb2TreeView::Fb2TreeView(Fb2WebView &view, QWidget *parent)
     , m_view(view)
     , m_view(view)
 {
 {
     setHeaderHidden(true);
     setHeaderHidden(true);
-    setContextMenuPolicy(Qt::ActionsContextMenu);
+    setContextMenuPolicy(Qt::CustomContextMenu);
 
 
     connect(this, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
     connect(this, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
     connect(m_view.page(), SIGNAL(loadFinished(bool)), SLOT(updateTree()));
     connect(m_view.page(), SIGNAL(loadFinished(bool)), SLOT(updateTree()));
     connect(m_view.page(), SIGNAL(contentsChanged()), SLOT(contentsChanged()));
     connect(m_view.page(), SIGNAL(contentsChanged()), SLOT(contentsChanged()));
     connect(m_view.page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
     connect(m_view.page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
+    connect(this, SIGNAL(customContextMenuRequested(QPoint)), SLOT(contextMenu(QPoint)));
 
 
     m_timerSelect.setInterval(1000);
     m_timerSelect.setInterval(1000);
     m_timerSelect.setSingleShot(true);
     m_timerSelect.setSingleShot(true);
@@ -349,7 +351,7 @@ Fb2TreeView::Fb2TreeView(Fb2WebView &view, QWidget *parent)
     QMetaObject::invokeMethod(this, "updateTree", Qt::QueuedConnection);
     QMetaObject::invokeMethod(this, "updateTree", Qt::QueuedConnection);
 }
 }
 
 
-void Fb2TreeView::initToolbar(QToolBar *toolbar)
+void Fb2TreeView::initActions(QToolBar *toolbar)
 {
 {
     QAction * act;
     QAction * act;
 
 
@@ -359,7 +361,7 @@ void Fb2TreeView::initToolbar(QToolBar *toolbar)
     act->setPriority(QAction::LowPriority);
     act->setPriority(QAction::LowPriority);
     connect(act, SIGNAL(triggered()), SLOT(insertNode()));
     connect(act, SIGNAL(triggered()), SLOT(insertNode()));
     toolbar->addAction(act);
     toolbar->addAction(act);
-    addAction(act);
+    m_menu.addAction(act);
 
 
     act = new QAction(FB2::icon("list-remove"), tr("&Delete"), this);
     act = new QAction(FB2::icon("list-remove"), tr("&Delete"), this);
     act->setShortcutContext(Qt::WidgetShortcut);
     act->setShortcutContext(Qt::WidgetShortcut);
@@ -367,37 +369,65 @@ void Fb2TreeView::initToolbar(QToolBar *toolbar)
     act->setPriority(QAction::LowPriority);
     act->setPriority(QAction::LowPriority);
     connect(act, SIGNAL(triggered()), SLOT(deleteNode()));
     connect(act, SIGNAL(triggered()), SLOT(deleteNode()));
     toolbar->addAction(act);
     toolbar->addAction(act);
-    addAction(act);
+    m_menu.addAction(act);
+
+    m_menu.addSeparator();
+
+    actionCut = act = new QAction(FB2::icon("edit-cut"), tr("Cu&t"), this);
+    act->setShortcutContext(Qt::WidgetShortcut);
+    act->setPriority(QAction::LowPriority);
+    act->setShortcuts(QKeySequence::Cut);
+    act->setEnabled(false);
+    m_menu.addAction(act);
+
+    actionCopy = act = new QAction(FB2::icon("edit-copy"), tr("&Copy"), this);
+    act->setShortcutContext(Qt::WidgetShortcut);
+    act->setPriority(QAction::LowPriority);
+    act->setShortcuts(QKeySequence::Copy);
+    act->setEnabled(false);
+    m_menu.addAction(act);
+
+    actionPaste = act = new QAction(FB2::icon("edit-paste"), tr("&Paste"), this);
+    act->setShortcutContext(Qt::WidgetShortcut);
+    act->setPriority(QAction::LowPriority);
+    act->setShortcuts(QKeySequence::Paste);
+    m_menu.addAction(act);
 
 
     toolbar->addSeparator();
     toolbar->addSeparator();
+    m_menu.addSeparator();
 
 
     act = new QAction(FB2::icon("go-up"), tr("&Up"), this);
     act = new QAction(FB2::icon("go-up"), tr("&Up"), this);
     act->setShortcutContext(Qt::WidgetShortcut);
     act->setShortcutContext(Qt::WidgetShortcut);
     act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
     act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
     connect(act, SIGNAL(triggered()), SLOT(moveUp()));
     connect(act, SIGNAL(triggered()), SLOT(moveUp()));
     toolbar->addAction(act);
     toolbar->addAction(act);
-    addAction(act);
+    m_menu.addAction(act);
 
 
     act = new QAction(FB2::icon("go-down"), tr("&Down"), this);
     act = new QAction(FB2::icon("go-down"), tr("&Down"), this);
     act->setShortcutContext(Qt::WidgetShortcut);
     act->setShortcutContext(Qt::WidgetShortcut);
     act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
     act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
     connect(act, SIGNAL(triggered()), SLOT(moveDown()));
     connect(act, SIGNAL(triggered()), SLOT(moveDown()));
     toolbar->addAction(act);
     toolbar->addAction(act);
-    addAction(act);
+    m_menu.addAction(act);
 
 
     act = new QAction(FB2::icon("go-previous"), tr("&Left"), this);
     act = new QAction(FB2::icon("go-previous"), tr("&Left"), this);
     act->setShortcutContext(Qt::WidgetShortcut);
     act->setShortcutContext(Qt::WidgetShortcut);
     act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
     act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
     connect(act, SIGNAL(triggered()), SLOT(moveLeft()));
     connect(act, SIGNAL(triggered()), SLOT(moveLeft()));
     toolbar->addAction(act);
     toolbar->addAction(act);
-    addAction(act);
+    m_menu.addAction(act);
 
 
     act = new QAction(FB2::icon("go-next"), tr("&Right"), this);
     act = new QAction(FB2::icon("go-next"), tr("&Right"), this);
     act->setShortcutContext(Qt::WidgetShortcut);
     act->setShortcutContext(Qt::WidgetShortcut);
     act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Right));
     act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Right));
     connect(act, SIGNAL(triggered()), SLOT(moveRight()));
     connect(act, SIGNAL(triggered()), SLOT(moveRight()));
     toolbar->addAction(act);
     toolbar->addAction(act);
-    addAction(act);
+    m_menu.addAction(act);
+}
+
+void Fb2TreeView::contextMenu(const QPoint &pos)
+{
+    m_menu.exec(QCursor::pos());
 }
 }
 
 
 void Fb2TreeView::selectionChanged()
 void Fb2TreeView::selectionChanged()
@@ -542,11 +572,10 @@ Fb2TreeWidget::Fb2TreeWidget(Fb2WebView &view, QWidget* parent)
     layout->setObjectName(QString::fromUtf8("verticalLayout"));
     layout->setObjectName(QString::fromUtf8("verticalLayout"));
 
 
     m_tree = new Fb2TreeView(view, this);
     m_tree = new Fb2TreeView(view, this);
-    m_tree->setContextMenuPolicy(Qt::ActionsContextMenu);
     layout->addWidget(m_tree);
     layout->addWidget(m_tree);
 
 
     m_tool = new QToolBar(this);
     m_tool = new QToolBar(this);
     layout->addWidget(m_tool);
     layout->addWidget(m_tool);
 
 
-    m_tree->initToolbar(m_tool);
+    m_tree->initActions(m_tool);
 }
 }

+ 12 - 1
source/fb2tree.hpp

@@ -2,12 +2,17 @@
 #define FB2TREE_HPP
 #define FB2TREE_HPP
 
 
 #include <QAbstractItemModel>
 #include <QAbstractItemModel>
+#include <QMenu>
 #include <QTreeView>
 #include <QTreeView>
 #include <QTimer>
 #include <QTimer>
 #include <QToolBar>
 #include <QToolBar>
 
 
 #include "fb2html.h"
 #include "fb2html.h"
 
 
+QT_BEGIN_NAMESPACE
+class QAction;
+QT_END_NAMESPACE
+
 class Fb2WebView;
 class Fb2WebView;
 
 
 class Fb2TreeModel;
 class Fb2TreeModel;
@@ -111,13 +116,14 @@ class Fb2TreeView : public QTreeView
 
 
 public:
 public:
     explicit Fb2TreeView(Fb2WebView &view, QWidget *parent = 0);
     explicit Fb2TreeView(Fb2WebView &view, QWidget *parent = 0);
-    void initToolbar(QToolBar *toolbar);
+    void initActions(QToolBar *toolbar);
 
 
 public slots:
 public slots:
     void updateTree();
     void updateTree();
 
 
 private slots:
 private slots:
     void activated(const QModelIndex &index);
     void activated(const QModelIndex &index);
+    void contextMenu(const QPoint &pos);
     void contentsChanged();
     void contentsChanged();
     void selectionChanged();
     void selectionChanged();
     void selectTree();
     void selectTree();
@@ -138,6 +144,11 @@ private:
     Fb2WebView & m_view;
     Fb2WebView & m_view;
     QTimer m_timerSelect;
     QTimer m_timerSelect;
     QTimer m_timerUpdate;
     QTimer m_timerUpdate;
+    QMenu m_menu;
+    QAction
+        *actionCut,
+        *actionCopy,
+        *actionPaste;
 };
 };
 
 
 class Fb2TreeWidget : public QWidget
 class Fb2TreeWidget : public QWidget

BIN
source/ts/ru.qm


+ 18 - 6
source/ts/ru.ts

@@ -516,27 +516,39 @@
     <name>Fb2TreeView</name>
     <name>Fb2TreeView</name>
     <message>
     <message>
         <source>&amp;Insert</source>
         <source>&amp;Insert</source>
-        <translation type="unfinished">&amp;Вставка</translation>
+        <translation>&amp;Вставка</translation>
     </message>
     </message>
     <message>
     <message>
         <source>&amp;Delete</source>
         <source>&amp;Delete</source>
-        <translation type="unfinished">&amp;Удалить</translation>
+        <translation>&amp;Удалить</translation>
     </message>
     </message>
     <message>
     <message>
         <source>&amp;Up</source>
         <source>&amp;Up</source>
-        <translation type="unfinished">В&amp;верх</translation>
+        <translation>В&amp;верх</translation>
     </message>
     </message>
     <message>
     <message>
         <source>&amp;Down</source>
         <source>&amp;Down</source>
-        <translation type="unfinished">В&amp;низ</translation>
+        <translation>В&amp;низ</translation>
     </message>
     </message>
     <message>
     <message>
         <source>&amp;Left</source>
         <source>&amp;Left</source>
-        <translation type="unfinished"></translation>
+        <translation>В&amp;лево</translation>
     </message>
     </message>
     <message>
     <message>
         <source>&amp;Right</source>
         <source>&amp;Right</source>
-        <translation type="unfinished"></translation>
+        <translation>В&amp;право</translation>
+    </message>
+    <message>
+        <source>Cu&amp;t</source>
+        <translation>Вы&amp;резать</translation>
+    </message>
+    <message>
+        <source>&amp;Copy</source>
+        <translation>&amp;Копировать</translation>
+    </message>
+    <message>
+        <source>&amp;Paste</source>
+        <translation>&amp;Вставить</translation>
     </message>
     </message>
 </context>
 </context>
 <context>
 <context>