File indexing completed on 2024-12-22 04:17:46
0001 /*************************************************************************** 0002 * * 0003 * copyright : (C) 2007 The University of Toronto * 0004 * netterfield@astro.utoronto.ca * 0005 * * 0006 * This program is free software; you can redistribute it and/or modify * 0007 * it under the terms of the GNU General Public License as published by * 0008 * the Free Software Foundation; either version 2 of the License, or * 0009 * (at your option) any later version. * 0010 * * 0011 ***************************************************************************/ 0012 0013 #include "tabwidget.h" 0014 #include "mainwindow.h" 0015 #include "view.h" 0016 #include "viewitem.h" 0017 #include "curveplacement.h" 0018 #include "plotitem.h" 0019 #include "plotitemmanager.h" 0020 #include "plotrenderitem.h" 0021 0022 #include <QInputDialog> 0023 #include <QMenu> 0024 #include <QTabBar> 0025 #include <QUndoGroup> 0026 #include <QDebug> 0027 #include <QDragEnterEvent> 0028 0029 0030 namespace Kst { 0031 0032 class TabBar : public QTabBar 0033 { 0034 public: 0035 TabBar(); 0036 void dragEnterEvent(QDragEnterEvent* event); 0037 void dragMoveEvent(QDragMoveEvent* event); 0038 void dropEvent(QDropEvent* event); 0039 }; 0040 0041 0042 TabBar::TabBar() 0043 { 0044 setAcceptDrops(true); 0045 } 0046 0047 void TabBar::dragEnterEvent(QDragEnterEvent* event) 0048 { 0049 if (MimeDataViewItem::downcast(event->mimeData())) { 0050 setCurrentIndex(tabAt(event->pos())); 0051 event->acceptProposedAction(); 0052 } 0053 } 0054 0055 void TabBar::dragMoveEvent(QDragMoveEvent* event) 0056 { 0057 if (MimeDataViewItem::downcast(event->mimeData())) { 0058 setCurrentIndex(tabAt(event->pos())); 0059 event->acceptProposedAction(); 0060 } 0061 } 0062 0063 void TabBar::dropEvent(QDropEvent* event) 0064 { 0065 event->setDropAction(Qt::IgnoreAction); 0066 event->accept(); 0067 } 0068 0069 0070 0071 TabWidget::TabWidget(QWidget *parent) 0072 : QTabWidget(parent) { 0073 setTabBar(new TabBar); 0074 tabBar()->setContextMenuPolicy(Qt::CustomContextMenu); 0075 connect(tabBar(), SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenu(QPoint))); 0076 _cnt = 0; 0077 #if QT_VERSION >= 0x040500 0078 tabBar()->setMovable(true); 0079 tabBar()->setExpanding(true); 0080 #endif 0081 } 0082 0083 0084 TabWidget::~TabWidget() { 0085 } 0086 0087 0088 View *TabWidget::createView() { 0089 View *view = new View; 0090 addView(view); 0091 return view; 0092 } 0093 0094 0095 void TabWidget::checkedShowTabbar() { 0096 if (count() < 2) { 0097 tabBar()->hide(); 0098 } else { 0099 tabBar()->show(); 0100 } 0101 } 0102 0103 void TabWidget::addView(View* view) { 0104 MainWindow *parent = qobject_cast<MainWindow*>(this->parent()); 0105 if (parent) { 0106 parent->undoGroup()->addStack(view->undoStack()); 0107 parent->undoGroup()->setActiveStack(view->undoStack()); 0108 } 0109 0110 connect(view, SIGNAL(viewModeChanged(View::ViewMode)), this, SIGNAL(currentViewModeChanged())); 0111 0112 QString label = view->objectName().isEmpty() ? 0113 tr("View &%1").arg(++_cnt) : 0114 view->objectName(); 0115 0116 addTab(view, label); 0117 checkedShowTabbar(); 0118 setCurrentWidget(view); 0119 } 0120 0121 0122 View *TabWidget::currentView() const { 0123 return qobject_cast<View*>(currentWidget()); 0124 } 0125 0126 0127 QList<View*> TabWidget::views() const { 0128 QList<View*> v; 0129 for (int i = 0; i < count(); ++i) { 0130 v.append(qobject_cast<View*>(widget(i))); 0131 } 0132 return v; 0133 } 0134 0135 0136 void TabWidget::deleteView(View* view) { 0137 MainWindow *parent = qobject_cast<MainWindow*>(this->parent()); 0138 PlotItemManager::self()->clearAllTiedZoom(view); 0139 if (parent) { 0140 parent->undoGroup()->removeStack(view->undoStack()); 0141 } 0142 removeTab(indexOf(view)); 0143 delete view; 0144 checkedShowTabbar(); 0145 } 0146 0147 0148 void TabWidget::clear() { 0149 QList<View*> tabs = views(); 0150 foreach(View* view, tabs) { 0151 deleteView(view); 0152 } 0153 _cnt = 0; 0154 checkedShowTabbar(); 0155 } 0156 0157 0158 void TabWidget::closeCurrentView() { 0159 deleteView(currentView()); 0160 if (count() == 0) 0161 createView(); 0162 } 0163 0164 0165 void TabWidget::renameCurrentView() { 0166 QTabBar *tb = tabBar(); 0167 int idx = tb->currentIndex(); 0168 bool ok = false; 0169 QString rc = QInputDialog::getText(this, tr("Rename Tab"), tr("Enter a new tab name:"), QLineEdit::Normal, tb->tabText(idx), &ok); 0170 if (ok) { 0171 tb->setTabText(idx, rc); 0172 } 0173 } 0174 0175 void TabWidget::setCurrentViewName(QString name) { 0176 int idx = tabBar()->currentIndex(); 0177 tabBar()->setTabText(idx, name); 0178 } 0179 0180 void TabWidget::contextMenu(const QPoint& pos) { 0181 QTabBar *tb = tabBar(); 0182 int idx = tb->currentIndex(); 0183 for (int i = 0; i < tb->count(); ++i) { 0184 if (tb->tabRect(i).contains(pos)) { 0185 idx = i; 0186 tb->setCurrentIndex(i); 0187 break; 0188 } 0189 } 0190 const QString txt = tb->tabText(idx); 0191 QMenu m(txt, tb); 0192 m.addAction(tr("&Add tab"), this, SLOT(createView())); 0193 m.addAction(tr("&Rename tab"), this, SLOT(renameCurrentView())); 0194 m.addAction(tr("&Close tab"), this, SLOT(closeCurrentView())); 0195 m.addSeparator(); 0196 m.addAction(tr("&Edit View"), currentView(), SLOT(edit())); 0197 0198 QMenu layoutMenu; 0199 layoutMenu.setTitle(tr("Cleanup Layout")); 0200 layoutMenu.addAction(tr("Automatic"), currentView(), SLOT(createLayout())); 0201 layoutMenu.addAction(tr("Custom"), currentView(), SLOT(createCustomLayout())); 0202 m.addMenu(&layoutMenu); 0203 0204 m.exec(tb->mapToGlobal(pos)); 0205 } 0206 0207 } 0208 0209 // vim: ts=2 sw=2 et