File indexing completed on 2024-04-21 14:56:13

0001 #include <QLayout>
0002 #include <QKeyEvent>
0003 #include <QMenu>
0004 #include <QMimeData>
0005 #include <QDrag>
0006 #include <qinputdialog.h>
0007 #include <QBoxLayout>
0008 #include <QApplication>
0009 
0010 #include <kdebug.h>
0011 #include <kiconloader.h>
0012 
0013 #include "ktabwidgettest.h"
0014 
0015 Test::Test(QWidget *parent)
0016     : QWidget(parent), mChange(0), mLeftWidget(nullptr), mRightWidget(nullptr),
0017       mLeftPopup(nullptr), mRightPopup(nullptr), mTabbarContextPopup(nullptr), mContextPopup(nullptr)
0018 
0019 {
0020     resize(600, 300);
0021 
0022     QVBoxLayout *topLayout = new QVBoxLayout(this);
0023     topLayout->setContentsMargins(0, 0, 0, 0);
0024     topLayout->setSpacing(0);
0025 
0026     mWidget = new KTabWidget(this);
0027     mWidget->addTab(new QLabel("Testlabel 1", nullptr), "&One");
0028     mWidget->addTab(new QLabel("Testlabel 2", nullptr), "Two");
0029     mWidget->addTab(new QWidget(), SmallIcon("konsole"), "Three");
0030     mWidget->addTab(new QWidget(), "Four");
0031     mWidget->setTabTextColor(0, Qt::red);
0032     mWidget->setTabTextColor(1, Qt::blue);
0033     mWidget->setUsesScrollButtons(false);   // corresponding checkbox is unchecked by default
0034 
0035     connect(mWidget, SIGNAL(currentChanged(QWidget*)), SLOT(currentChanged(QWidget*)));
0036     connect(mWidget, SIGNAL(contextMenu(QWidget*,QPoint)), SLOT(contextMenu(QWidget*,QPoint)));
0037     connect(mWidget, SIGNAL(contextMenu(QPoint)), SLOT(tabbarContextMenu(QPoint)));
0038     connect(mWidget, SIGNAL(mouseDoubleClick(QWidget*)), SLOT(mouseDoubleClick(QWidget*)));
0039     connect(mWidget, SIGNAL(mouseMiddleClick()), SLOT(addTab()));
0040     connect(mWidget, SIGNAL(mouseMiddleClick(QWidget*)), SLOT(mouseMiddleClick(QWidget*)));
0041     connect(mWidget, SIGNAL(closeRequest(QWidget*)), SLOT(mouseMiddleClick(QWidget*)));
0042     connect(mWidget, SIGNAL(testCanDecode(const QDragMoveEvent*,bool&)), SLOT(testCanDecode(const QDragMoveEvent*,bool&)));
0043     connect(mWidget, SIGNAL(receivedDropEvent(QDropEvent*)), SLOT(receivedDropEvent(QDropEvent*)));
0044     connect(mWidget, SIGNAL(receivedDropEvent(QWidget*,QDropEvent*)), SLOT(receivedDropEvent(QWidget*,QDropEvent*)));
0045     connect(mWidget, SIGNAL(initiateDrag(QWidget*)), SLOT(initiateDrag(QWidget*)));
0046     connect(mWidget, SIGNAL(movedTab(int,int)), SLOT(movedTab(int,int)));
0047     mWidget->setMovable(true);
0048 
0049     topLayout->addWidget(mWidget);
0050 
0051     QWidget *grid = new QWidget(this);
0052     topLayout->addWidget(grid);
0053     QGridLayout *gridlayout = new QGridLayout(grid);
0054 
0055     QPushButton *addTab = new QPushButton("Add Tab", grid);
0056     gridlayout->addWidget(addTab, 0, 0);
0057     connect(addTab, SIGNAL(clicked()), SLOT(addTab()));
0058 
0059     QPushButton *removeTab = new QPushButton("Remove Current Tab", grid);
0060     gridlayout->addWidget(removeTab, 0, 1);
0061     connect(removeTab, SIGNAL(clicked()), SLOT(removeCurrentTab()));
0062 
0063     mLeftButton = new QCheckBox("Show left button", grid);
0064     gridlayout->addWidget(mLeftButton, 1, 0);
0065     connect(mLeftButton, SIGNAL(toggled(bool)), SLOT(toggleLeftButton(bool)));
0066     mLeftButton->setChecked(true);
0067 
0068     QCheckBox *leftPopup = new QCheckBox("Enable left popup", grid);
0069     gridlayout->addWidget(leftPopup, 2, 0);
0070     connect(leftPopup, SIGNAL(toggled(bool)), SLOT(toggleLeftPopup(bool)));
0071     leftPopup->setChecked(true);
0072 
0073     mRightButton = new QCheckBox("Show right button", grid);
0074     gridlayout->addWidget(mRightButton, 1, 1);
0075     connect(mRightButton, SIGNAL(toggled(bool)), SLOT(toggleRightButton(bool)));
0076     mRightButton->setChecked(true);
0077 
0078     QCheckBox *rightPopup = new QCheckBox("Enable right popup", grid);
0079     gridlayout->addWidget(rightPopup, 2, 1);
0080     connect(rightPopup, SIGNAL(toggled(bool)), SLOT(toggleRightPopup(bool)));
0081     rightPopup->setChecked(true);
0082 
0083     mTabsBottom = new QCheckBox("Show tabs at bottom", grid);
0084     gridlayout->addWidget(mTabsBottom, 3, 0);
0085     connect(mTabsBottom, SIGNAL(toggled(bool)), SLOT(toggleTabPosition(bool)));
0086 
0087     QCheckBox *tabshape = new QCheckBox("Triangular tab shape", grid);
0088     gridlayout->addWidget(tabshape, 3, 1);
0089     connect(tabshape, SIGNAL(toggled(bool)), SLOT(toggleTabShape(bool)));
0090 
0091     QCheckBox *tabClose = new QCheckBox("Close button on icon hover", grid);
0092     gridlayout->addWidget(tabClose, 4, 0);
0093     connect(tabClose, SIGNAL(toggled(bool)), SLOT(toggleCloseButtons(bool)));
0094     tabClose->setChecked(true);
0095 
0096     QCheckBox *showlabels = new QCheckBox("Show labels", grid);
0097     gridlayout->addWidget(showlabels, 4, 1);
0098     connect(showlabels, SIGNAL(toggled(bool)), this, SLOT(toggleLabels(bool)));
0099 
0100     QCheckBox *elideText = new QCheckBox("Elide text", grid);
0101     gridlayout->addWidget(elideText, 5, 0);
0102     connect(elideText, SIGNAL(toggled(bool)), this, SLOT(toggleEliding(bool)));
0103 
0104     QCheckBox *scrollButtons = new QCheckBox("Enable scroll buttons", grid);
0105     gridlayout->addWidget(scrollButtons, 5, 1);
0106     connect(scrollButtons, SIGNAL(toggled(bool)), this, SLOT(toggleScrollButtons(bool)));
0107 }
0108 
0109 void Test::currentChanged(QWidget *w)
0110 {
0111     mWidget->setTabTextColor(mWidget->indexOf(w), Qt::black);
0112 }
0113 
0114 void Test::addTab()
0115 {
0116     mWidget->addTab(new QWidget(), SmallIcon("konsole"), QString("This is tab %1").arg(mWidget->count() + 1));
0117 }
0118 
0119 void Test::testCanDecode(const QDragMoveEvent *e, bool &accept /* result */)
0120 {
0121     if (e->mimeData()->hasText()) {    // don't accept=false if it cannot be decoded!
0122         accept = true;
0123     }
0124 }
0125 
0126 void Test::receivedDropEvent(QDropEvent *e)
0127 {
0128     if (e->mimeData()->hasText()) {
0129         mWidget->addTab(new QWidget(), e->mimeData()->text());
0130     }
0131 }
0132 
0133 void Test::receivedDropEvent(QWidget *w, QDropEvent *e)
0134 {
0135     if (e->mimeData()->hasText()) {
0136         mWidget->setTabText(mWidget->indexOf(w), e->mimeData()->text());
0137     }
0138 }
0139 
0140 void Test::initiateDrag(QWidget *w)
0141 {
0142     QDrag *drag = new QDrag(this);
0143     QMimeData *mimeData = new QMimeData;
0144     mimeData->setText(mWidget->tabText(mWidget->indexOf(w)));
0145     drag->setMimeData(mimeData);
0146     drag->start(); // do NOT delete d.
0147 }
0148 
0149 void Test::removeCurrentTab()
0150 {
0151     if (mWidget->count() == 1) {
0152         return;
0153     }
0154 
0155     mWidget->removeTab(mWidget->currentIndex());
0156 }
0157 
0158 void Test::toggleLeftButton(bool state)
0159 {
0160     if (state) {
0161         if (!mLeftWidget) {
0162             mLeftWidget = new QToolButton(mWidget);
0163             connect(mLeftWidget, SIGNAL(clicked()), SLOT(addTab()));
0164             mLeftWidget->setIcon(SmallIcon("tab-new"));
0165             mLeftWidget->setText("New");
0166             mLeftWidget->setToolTip("New");
0167             mLeftWidget->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
0168             mLeftWidget->adjustSize();
0169             //mLeftWidget->setGeometry( 0, 0, h, h );
0170             mLeftWidget->setMenu(mLeftPopup);
0171             mWidget->setCornerWidget(mLeftWidget, Qt::TopLeftCorner);
0172         }
0173         mLeftWidget->show();
0174     } else {
0175         mLeftWidget->hide();
0176     }
0177 }
0178 
0179 void Test::toggleLeftPopup(bool state)
0180 {
0181     if (state) {
0182         if (!mLeftPopup) {
0183             mLeftPopup = new QMenu(this);
0184             mLeftPopup->addAction(SmallIcon("tab-new"), "Empty Tab");
0185             mLeftPopup->addAction(SmallIcon("tab-new"), "Empty Tab After First");
0186             mLeftPopup->addSeparator();
0187             mLeftPopup->addAction(SmallIcon("tab-new"), "Button Tab");
0188             mLeftPopup->addAction(SmallIcon("tab-new"), "Label Tab");
0189             connect(mLeftPopup, SIGNAL(triggered(QAction*)), SLOT(leftPopupActivated(QAction*)));
0190         }
0191         mLeftWidget->setMenu(mLeftPopup);
0192     } else {
0193         mLeftWidget->setMenu(nullptr);
0194     }
0195 }
0196 
0197 void Test::leftPopupActivated(QAction *action)
0198 {
0199     switch (mLeftPopup->actions().indexOf(action)) {
0200     case 0: mWidget->addTab(new QWidget(), QString("Tab %1").arg(mWidget->count() + 1));
0201         break;
0202     case 1: mWidget->insertTab(1, new QWidget(), QString("Tab %1").arg(mWidget->count() + 1));
0203         break;
0204     case 3: mWidget->addTab(new QPushButton("Testbutton"), QString("Tab %1").arg(mWidget->count() + 1));
0205         break;
0206     case 4: mWidget->addTab(new QLabel("Testlabel"), QString("Tab %1").arg(mWidget->count() + 1));
0207         break;
0208     }
0209 }
0210 
0211 void Test::toggleRightButton(bool state)
0212 {
0213     if (state) {
0214         if (!mRightWidget) {
0215             mRightWidget = new QToolButton(mWidget);
0216             QObject::connect(mRightWidget, SIGNAL(clicked()), SLOT(removeCurrentTab()));
0217             mRightWidget->setIcon(SmallIcon("tab-close"));
0218             mRightWidget->setText("Close");
0219             mRightWidget->setToolTip("Close");
0220             mRightWidget->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
0221             mRightWidget->adjustSize();
0222             //mRightButton->setGeometry( 0, 0, h, h );
0223             mRightWidget->setMenu(mRightPopup);
0224             mWidget->setCornerWidget(mRightWidget, Qt::TopRightCorner);
0225         }
0226         mRightWidget->show();
0227     } else {
0228         mRightWidget->hide();
0229     }
0230 }
0231 
0232 void Test::toggleRightPopup(bool state)
0233 {
0234     if (state) {
0235         if (!mRightPopup) {
0236             mRightPopup = new QMenu(this);
0237             mRightPopup->addAction(SmallIcon("tab-close"), "Current Tab");
0238             mRightPopup->addSeparator();
0239             mRightPopup->addAction(SmallIcon("tab-close"), "Most Left Tab");
0240             mRightPopup->addAction(SmallIcon("tab-close"), "Most Right Tab");
0241             connect(mRightPopup, SIGNAL(triggered(QAction*)), SLOT(rightPopupActivated(QAction*)));
0242         }
0243         mRightWidget->setMenu(mRightPopup);
0244     } else {
0245         mRightWidget->setMenu(nullptr);
0246     }
0247 }
0248 
0249 void Test::rightPopupActivated(QAction *action)
0250 {
0251     switch (mRightPopup->actions().indexOf(action)) {
0252     case 0: removeCurrentTab();
0253         break;
0254     case 2: if (mWidget->count() > 1) {
0255             mWidget->removeTab(0);
0256         }
0257         break;
0258     case 3: int count = mWidget->count();
0259         if (count > 1) {
0260             mWidget->removeTab(count - 1);
0261         }
0262         break;
0263     }
0264 }
0265 
0266 void Test::toggleTabPosition(bool state)
0267 {
0268     mWidget->setTabPosition(state ? QTabWidget::South : QTabWidget::North);
0269 }
0270 
0271 void Test::toggleTabShape(bool state)
0272 {
0273     mWidget->setTabShape(state ? QTabWidget::Triangular : QTabWidget::Rounded);
0274 }
0275 
0276 void Test::toggleCloseButtons(bool state)
0277 {
0278     mWidget->setTabsClosable(state);
0279 }
0280 
0281 void Test::contextMenu(QWidget *w, const QPoint &p)
0282 {
0283     delete mContextPopup;
0284 
0285     int idx = mWidget->indexOf(w);
0286     mContextPopup = new QMenu(this);
0287     mContextPopup->addAction("Activate Tab");
0288     mContextPopup->addSeparator();
0289     mContextPopup->addAction(SmallIcon("konsole"), "Set This Icon");
0290     mContextPopup->addAction(SmallIcon("konqueror"), "Set This Icon");
0291     mContextPopup->addSeparator();
0292     mContextPopup->addAction(mWidget->isTabEnabled(idx) ? "Disable Tab" : "Enable Tab");
0293     mContextPopup->addAction(mWidget->tabToolTip(idx).isEmpty() ? "Set Tooltip" : "Remove Tooltip");
0294     connect(mContextPopup, SIGNAL(triggered(QAction*)), SLOT(contextMenuActivated(QAction*)));
0295 
0296     mContextWidgetIndex = mWidget->indexOf(w);
0297     mContextPopup->popup(p);
0298 }
0299 
0300 void Test::contextMenuActivated(QAction *action)
0301 {
0302     switch (mContextPopup->actions().indexOf(action)) {
0303     case 0:
0304         mWidget->setCurrentIndex(mContextWidgetIndex);
0305         break;
0306     case 2:
0307         mWidget->setTabIcon(mContextWidgetIndex, SmallIcon("konsole"));
0308         break;
0309     case 3:
0310         mWidget->setTabIcon(mContextWidgetIndex, SmallIcon("konqueror"));
0311         break;
0312     case 4:
0313         mWidget->setTabEnabled(mContextWidgetIndex, !(mWidget->isTabEnabled(mContextWidgetIndex)));
0314         break;
0315     case 5:
0316         if (mWidget->tabToolTip(mContextWidgetIndex).isEmpty()) {
0317             mWidget->setTabToolTip(mContextWidgetIndex, "This is a tool tip.");
0318         } else {
0319             mWidget->setTabToolTip(mContextWidgetIndex, QString());
0320         }
0321         break;
0322     }
0323 }
0324 
0325 void Test::tabbarContextMenu(const QPoint &p)
0326 {
0327     delete mTabbarContextPopup;
0328 
0329     mTabbarContextPopup = new QMenu(this);
0330     mTabbarContextPopup->addAction(SmallIcon("tab-new"), mLeftWidget->isVisible() ? "Hide \"Add\" Button" : "Show \"Add\" Button");
0331     mTabbarContextPopup->addAction(SmallIcon("tab-close"), mRightWidget->isVisible() ? "Hide \"Remove\" Button" : "Show \"Remove\" Button");
0332     mTabbarContextPopup->addSeparator();
0333     mTabbarContextPopup->addAction(mWidget->tabPosition() == QTabWidget::North ? "Put Tabbar to Bottom" : "Put Tabbar to Top");
0334     connect(mTabbarContextPopup, SIGNAL(triggered(QAction*)), SLOT(tabbarContextMenuActivated(QAction*)));
0335 
0336     mTabbarContextPopup->popup(p);
0337 }
0338 
0339 void Test::tabbarContextMenuActivated(QAction *action)
0340 {
0341     switch (mTabbarContextPopup->actions().indexOf(action)) {
0342     case 0: mLeftButton->toggle();
0343         break;
0344     case 1: mRightButton->toggle();
0345         break;
0346     case 3: mTabsBottom->toggle();
0347         break;
0348     }
0349 }
0350 
0351 void Test::mouseDoubleClick(QWidget *w)
0352 {
0353     int index = mWidget->indexOf(w);
0354     bool ok;
0355     QString text = QInputDialog::getText(this, "Rename Tab", "Enter new name:",
0356                                          QLineEdit::Normal, mWidget->tabText(index), &ok);
0357     if (ok && !text.isEmpty()) {
0358         mWidget->setTabText(index, text);
0359         mWidget->setTabTextColor(index, Qt::green);
0360     }
0361 }
0362 
0363 void Test::mouseMiddleClick(QWidget *w)
0364 {
0365     if (mWidget->count() == 1) {
0366         return;
0367     }
0368 
0369     mWidget->removeTab(mWidget->indexOf(w));
0370 }
0371 
0372 void Test::movedTab(int from, int to)
0373 {
0374     kDebug() << "Moved tab from index " << from << " to " << to;
0375 }
0376 
0377 void Test::toggleLabels(bool state)
0378 {
0379     mLeftWidget->setToolButtonStyle(state ? Qt::ToolButtonTextUnderIcon : Qt::ToolButtonIconOnly);
0380     mLeftWidget->adjustSize();
0381     mRightWidget->setToolButtonStyle(state ? Qt::ToolButtonTextUnderIcon : Qt::ToolButtonIconOnly);
0382     mRightWidget->adjustSize();
0383     mWidget->hide();   // trigger update
0384     mWidget->show();
0385 }
0386 
0387 void Test::toggleScrollButtons(bool state)
0388 {
0389     mWidget->setUsesScrollButtons(state);
0390 }
0391 
0392 void Test::toggleEliding(bool state)
0393 {
0394     mWidget->setAutomaticResizeTabs(state);
0395     //mWidget->setElideMode(state ? Qt::ElideRight : Qt::ElideNone);
0396 }
0397 
0398 int main(int argc, char **argv)
0399 {
0400     QApplication::setApplicationName("ktabwidgettest");
0401     QApplication app(argc, argv);
0402     Test *t = new Test();
0403     t->show();
0404     app.exec();
0405 }
0406 
0407 #include "moc_ktabwidgettest.cpp"