[Kst] branches/work/kst/portto4/kst/src/libkstapp

George Staikos staikos at kde.org
Mon May 28 22:26:03 CEST 2007


SVN commit 669234 by staikos:

hook in the context menu for tabs


 M  +34 -0     tabwidget.cpp  
 M  +2 -0      tabwidget.h  


--- branches/work/kst/portto4/kst/src/libkstapp/tabwidget.cpp #669233:669234
@@ -13,12 +13,17 @@
 #include "mainwindow.h"
 #include "view.h"
 
+#include <QInputDialog>
+#include <QMenu>
+#include <QTabBar>
 #include <QUndoGroup>
 
 namespace Kst {
 
 TabWidget::TabWidget(QWidget *parent)
 : QTabWidget(parent) {
+  tabBar()->setContextMenuPolicy(Qt::CustomContextMenu);
+  connect(tabBar(), SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(contextMenu(const QPoint&)));
 }
 
 
@@ -65,9 +70,38 @@
 }
 
 
+void TabWidget::renameCurrentView() {
+  QTabBar *tb = tabBar();
+  int idx = tb->currentIndex();
+  bool ok = false;
+  QString rc = QInputDialog::getText(this, tr("Rename Tab"), tr("Enter a new tab name:"), QLineEdit::Normal, tb->tabText(idx), &ok);
+  if (ok) {
+    tb->setTabText(idx, rc);
+  }
 }
 
 
+void TabWidget::contextMenu(const QPoint& pos) {
+  QTabBar *tb = tabBar();
+  int idx = tb->currentIndex();
+  for (int i = 0; i < tb->count(); ++i) {
+    if (tb->tabRect(i).contains(pos)) {
+       idx = i;
+       tb->setCurrentIndex(i);
+       break;
+    }
+  }
+  const QString txt = tb->tabText(idx);
+  QMenu m(txt, tb);
+  m.addAction(tr("&Add tab"), this, SLOT(createView()));
+  m.addAction(tr("&Rename tab"), this, SLOT(renameCurrentView()));
+  m.addAction(tr("&Close tab"), this, SLOT(closeCurrentView()));
+  m.exec(tb->mapToGlobal(pos));
+}
+
+}
+
+
 #include "tabwidget.moc"
 
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/tabwidget.h #669233:669234
@@ -29,10 +29,12 @@
 
   public Q_SLOTS:
     View *createView();
+    void renameCurrentView();
     void closeCurrentView();
 
   private Q_SLOTS:
     void viewDestroyed(QObject *object);
+    void contextMenu(const QPoint&);
 };
 
 }


More information about the Kst mailing list