]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/TocWidget.cpp
TOC context menu (part 3)
[lyx.git] / src / frontends / qt4 / TocWidget.cpp
index a47d5daa2e37d104bc3d0aa6ac7b55b013bd75a1..688c7fe74a06ce26554e48474b125b753511c000 100644 (file)
 #include "TocModel.h"
 
 #include "Buffer.h"
+#include "CutAndPaste.h"
 #include "FuncRequest.h"
 #include "LyXFunc.h"
+#include "Menus.h"
+#include "TocBackend.h"
+
+#include "insets/InsetCommand.h"
+#include "insets/InsetRef.h"
 
 #include "support/debug.h"
 #include "support/lassert.h"
 
 #include <QHeaderView>
+#include <QMenu>
 #include <QTimer>
 
 #include <vector>
@@ -67,10 +74,56 @@ TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
        // Buffer.
        enableControls(false);
 
+       // make us responsible for the context menu of the tabbar
+       setContextMenuPolicy(Qt::CustomContextMenu);
+       connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
+               this, SLOT(showContextMenu(const QPoint &)));
+       connect(tocTV, SIGNAL(customContextMenuRequested(const QPoint &)),
+               this, SLOT(showContextMenu(const QPoint &)));
+
        init(QString());
 }
 
 
+void TocWidget::showContextMenu(const QPoint & pos)
+{
+       std::string name = "context-toc-" + fromqstr(current_type_);
+       QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
+       if (!menu)
+               return; 
+       menu->exec(mapToGlobal(pos));
+}
+
+
+void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
+{
+       switch(cmd.action) {
+       
+       case LFUN_COPY_LABEL_AS_REF:
+       case LFUN_INSERT_LABEL_AS_REF: {
+               QModelIndex index = tocTV->currentIndex();
+               TocItem const & item =
+                       gui_view_.tocModels().currentItem(current_type_, index);
+               if (!item.str().empty()) {
+                       InsetCommandParams p(REF_CODE, "ref");
+                       p["reference"] =  item.str();
+                       if (cmd.action == LFUN_COPY_LABEL_AS_REF) {
+                               cap::clearSelection();
+                               cap::copyInset(cur, new InsetRef(*cur.buffer(), p), item.str());
+                       } else {
+                               string const data = InsetCommand::params2string("ref", p);
+                               dispatch(FuncRequest(LFUN_INSET_INSERT, data));
+                       }
+               }
+               break;
+       }
+
+       default:
+               break;
+       }
+}
+
+
 void TocWidget::on_tocTV_activated(QModelIndex const & index)
 {
        goTo(index);