File indexing completed on 2024-10-13 12:19:30

0001 #include "kwindowtest.h"
0002 
0003 #include <QDir>
0004 #include <QMessageBox>
0005 #include <QTest>
0006 #include <QTextEdit>
0007 
0008 #include <cstdlib>
0009 
0010 #include <KActionMenu>
0011 #include <KToggleAction>
0012 #include <QAction>
0013 #include <QActionGroup>
0014 #include <QApplication>
0015 #include <QComboBox>
0016 #include <QLineEdit>
0017 #include <kactioncollection.h>
0018 #include <khelpmenu.h>
0019 #include <kxmlguifactory.h>
0020 
0021 TestWindow::TestWindow(QWidget *parent)
0022     : KXmlGuiWindow(parent)
0023 {
0024     ena = false;
0025     exitB = true; // exit button is shown
0026     lineL = true; // LineEdit is enabled
0027     greenF = false; // Frame not inserted
0028     timer = nullptr;
0029 
0030     setCaption(QStringLiteral("test window"));
0031 
0032     // The xmlgui file defines the layout of the menus and toolbars.
0033     // We only need to create actions with the right name here.
0034 
0035     // First four  buttons
0036     fileNewAction = new QAction(QIcon::fromTheme(QStringLiteral("document-new")), QStringLiteral("Create.. (toggles upper button)"), this);
0037     actionCollection()->addAction(QStringLiteral("filenew"), fileNewAction);
0038     fileNewAction->setCheckable(true);
0039     connect(fileNewAction, &QAction::triggered, this, &TestWindow::slotNew);
0040 
0041     QAction *fileOpenAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open")), QStringLiteral("Open"), this);
0042     actionCollection()->addAction(QStringLiteral("fileopen"), fileOpenAction);
0043     connect(fileOpenAction, &QAction::triggered, this, &TestWindow::slotOpen);
0044 
0045     KActionMenu *fileFloppyAction = new KActionMenu(QIcon::fromTheme(QStringLiteral("filefloppy")), QStringLiteral("Save (beep or delayed popup)"), this);
0046     actionCollection()->addAction(QStringLiteral("filefloppy"), fileFloppyAction);
0047     connect(fileFloppyAction, &QAction::triggered, this, &TestWindow::slotSave);
0048 
0049     QAction *filePrintAction = new QAction(QIcon::fromTheme(QStringLiteral("document-print")), QStringLiteral("Print (enables/disables open)"), this);
0050     actionCollection()->addAction(QStringLiteral("fileprint"), filePrintAction);
0051     filePrintAction->setToolTip(QStringLiteral("This tooltip does not work for menu items"));
0052     filePrintAction->setWhatsThis(QStringLiteral("This is the longer explanation of the action"));
0053     filePrintAction->setStatusTip(QStringLiteral("This action is supposed to print, but in fact enables/disables open"));
0054     connect(filePrintAction, &QAction::triggered, this, &TestWindow::slotPrint);
0055 
0056     // And a combobox
0057     // arguments: text (or strList), ID, writable, signal, object, slot, enabled,
0058     //            tooltiptext, size
0059     testComboBox = new QComboBox(toolBar());
0060     // K3WidgetAction* comboAction = new K3WidgetAction(testComboBox, QString(), 0, 0, 0, actionCollection(), "combobox");
0061     // connect(testComboBox, SIGNAL(activated(QString)), this, SLOT(slotList(QString)));
0062 
0063     // Then one line editor
0064     // arguments: text, id, signal, object (this), slot, enabled, tooltiptext, size
0065     testLineEdit = new QLineEdit(toolBar());
0066     testLineEdit->setText(QStringLiteral("ftp://ftp.kde.org/pub/kde"));
0067     //    K3WidgetAction* lineEditAction = new K3WidgetAction(testLineEdit, QString(), 0, 0, 0, actionCollection(), "location");
0068     //    connect(testLineEdit, SIGNAL(returnPressed()), this, SLOT(slotReturn()));
0069 
0070     // Now add another button and align it right
0071     exitAction = new QAction(QIcon::fromTheme(QStringLiteral("application-exit")), QStringLiteral("Exit"), this);
0072     actionCollection()->addAction(QStringLiteral("exit"), exitAction);
0073     connect(exitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
0074 
0075     // Another toolbar
0076 
0077     QAction *fileNewAction2 = new QAction(QIcon::fromTheme(QStringLiteral("document-new")), QStringLiteral("Create new file2 (Toggle)"), this);
0078     actionCollection()->addAction(QStringLiteral("filenew2"), fileNewAction2);
0079     connect(fileNewAction2, &QAction::toggled, this, &TestWindow::slotToggle);
0080     fileNewAction2->setToolTip(QStringLiteral("Tooltip"));
0081     fileNewAction2->setStatusTip(QStringLiteral("Statustip"));
0082     fileNewAction2->setWhatsThis(QStringLiteral("WhatsThis"));
0083 
0084     QAction *fileOpenAction2 = new QAction(QIcon::fromTheme(QStringLiteral("document-open")), QStringLiteral("Open (starts progress in sb)"), this);
0085     actionCollection()->addAction(QStringLiteral("fileopen2"), fileOpenAction2);
0086     connect(fileOpenAction2, &QAction::triggered, this, &TestWindow::slotOpen);
0087     fileOpenAction2->setToolTip(QStringLiteral("This action starts a progressbar inside the statusbar"));
0088 
0089     QAction *fileFloppyAction2 = new QAction(QIcon::fromTheme(QStringLiteral("filefloppy")), QStringLiteral("Save file2 (autorepeat)"), this);
0090     actionCollection()->addAction(QStringLiteral("filefloppy2"), fileFloppyAction2);
0091     connect(fileFloppyAction2, &QAction::triggered, this, &TestWindow::slotSave);
0092 
0093     itemsMenu = new QMenu;
0094     itemsMenu->addAction(QStringLiteral("delete/insert exit button"), this, &TestWindow::slotExit);
0095     itemsMenu->addAction(QStringLiteral("enable/disable lineedit"), this, &TestWindow::slotLined);
0096     itemsMenu->addAction(QStringLiteral("Toggle fileNew"), this, &TestWindow::slotNew);
0097     itemsMenu->addAction(QStringLiteral("Combo: clear"), this, &TestWindow::slotClearCombo);
0098     itemsMenu->addAction(QStringLiteral("Combo: insert list"), this, &TestWindow::slotInsertListInCombo);
0099     itemsMenu->addAction(QStringLiteral("Combo: make item 3 current"), this, &TestWindow::slotMakeItem3Current);
0100     itemsMenu->addAction(QStringLiteral("Important msg in statusbar"), this, &TestWindow::slotImportant);
0101 
0102     QAction *filePrintAction2 = new QAction(QIcon::fromTheme(QStringLiteral("document-print")), QStringLiteral("Print (pops menu)"), this);
0103     actionCollection()->addAction(QStringLiteral("fileprint2"), filePrintAction2);
0104     filePrintAction2->setMenu(itemsMenu);
0105 
0106     // *** RADIO BUTTONS
0107     QActionGroup *radioGroup = new QActionGroup(this);
0108     radioGroup->setExclusive(true);
0109 
0110     KToggleAction *radioButton1 = new KToggleAction(QIcon::fromTheme(QStringLiteral("document-new")), QStringLiteral("Radiobutton1"), this);
0111     actionCollection()->addAction(QStringLiteral("radioButton1"), radioButton1);
0112     radioButton1->setActionGroup(radioGroup);
0113 
0114     KToggleAction *radioButton2 = new KToggleAction(QIcon::fromTheme(QStringLiteral("document-open")), QStringLiteral("Radiobutton2"), this);
0115     actionCollection()->addAction(QStringLiteral("radioButton2"), radioButton2);
0116     radioButton2->setActionGroup(radioGroup);
0117 
0118     KToggleAction *radioButton3 = new KToggleAction(QIcon::fromTheme(QStringLiteral("filefloppy")), QStringLiteral("Radiobutton3"), this);
0119     actionCollection()->addAction(QStringLiteral("radioButton3"), radioButton3);
0120     radioButton3->setActionGroup(radioGroup);
0121 
0122     KToggleAction *radioButton4 = new KToggleAction(QIcon::fromTheme(QStringLiteral("document-print")), QStringLiteral("Radiobutton4"), this);
0123     actionCollection()->addAction(QStringLiteral("radioButton4"), radioButton4);
0124     radioButton4->setActionGroup(radioGroup);
0125 
0126     connect(radioGroup, &QActionGroup::triggered, this, &TestWindow::slotToggled);
0127 
0128     /**************************************************/
0129     /*Now, we setup statusbar; order is not important. */
0130     /**************************************************/
0131     statusBar = new QStatusBar(this);
0132     // statusBar->insertItem("Hi there!                         ", 0);
0133     // statusBar->insertItem("Look for tooltips to see functions", 1);
0134     setStatusBar(statusBar);
0135 
0136     // DigitalClock *clk = new DigitalClock (statusBar);
0137     // clk->setFrameStyle(QFrame::NoFrame);
0138     // statusBar->insertWidget(clk, 70, 2);
0139 
0140     // Set main widget. In this example it is Qt's multiline text editor.
0141     widget = new QTextEdit(this);
0142     setCentralWidget(widget);
0143 
0144     // Setup is now complete
0145 
0146     setAutoSaveSettings();
0147 
0148     // This is not strictly related to toolbars, menubars or KMainWindow.
0149     // Setup popup for completions
0150     completions = new QMenu;
0151 
0152     completions->addAction(QStringLiteral("/"));
0153     completions->addAction(QStringLiteral("/usr/"));
0154     completions->addAction(QStringLiteral("/lib/"));
0155     completions->addAction(QStringLiteral("/var/"));
0156     completions->addAction(QStringLiteral("/bin/"));
0157     completions->addAction(QStringLiteral("/kde/"));
0158     completions->addAction(QStringLiteral("/home/"));
0159     completions->addAction(QStringLiteral("/vmlinuz :-)"));
0160 
0161     connect(completions, &QMenu::triggered, this, &TestWindow::slotCompletionsMenu);
0162     pr = nullptr;
0163 
0164     // KXMLGUIClient looks in the "data" resource for the .rc files
0165     // This line is for test programs only!
0166     qputenv("XDG_DATA_HOME", QFile::encodeName(QFileInfo(QFINDTESTDATA("kwindowtest.rc")).absolutePath()));
0167     setupGUI(QSize(400, 500), Default, QStringLiteral("kwindowtest.rc"));
0168 
0169     tb = toolBar();
0170     tb1 = toolBar(QStringLiteral("AnotherToolBar"));
0171 }
0172 /***********************************/
0173 /*  Now slots for toolbar actions  */
0174 /***********************************/
0175 void TestWindow::slotToggled(QAction *)
0176 {
0177     statusBar->showMessage(QStringLiteral("Button toggled"), 1500);
0178 }
0179 
0180 void TestWindow::slotInsertClock()
0181 {
0182     // DigitalClock *clock = new DigitalClock(tb1);
0183     // clock->setFrameStyle(QFrame::NoFrame);
0184     // tb1->insertWidget(8, 70, clock);
0185 }
0186 
0187 void TestWindow::slotNew()
0188 {
0189     // tb1->actions()[0]->toggle();
0190     // toolBar()->removeAction( fileNewAction );
0191 }
0192 void TestWindow::slotOpen()
0193 {
0194     if (pr == nullptr) {
0195         pr = new QProgressBar(statusBar);
0196         pr->show();
0197     }
0198     //  statusBar->message(pr);
0199     if (!timer) {
0200         timer = new QTimer(this);
0201         connect(timer, &QTimer::timeout, this, &TestWindow::slotGoGoGoo);
0202     }
0203     timer->start(100);
0204 }
0205 
0206 void TestWindow::slotGoGoGoo()
0207 {
0208     pr->setValue(pr->value() + 1);
0209     if (pr->value() == 100) {
0210         timer->stop();
0211         statusBar->clearMessage();
0212         delete pr;
0213         pr = nullptr;
0214     }
0215 }
0216 
0217 void TestWindow::slotSave()
0218 {
0219     qApp->beep();
0220     statusBar->showMessage(QStringLiteral("Saving properties..."));
0221 }
0222 
0223 void TestWindow::slotPrint()
0224 {
0225     statusBar->showMessage(QStringLiteral("Print file pressed"));
0226     ena = !ena;
0227     qobject_cast<QAction *>(sender())->setEnabled(ena);
0228 }
0229 void TestWindow::slotReturn()
0230 {
0231     QString s = QStringLiteral("You entered ");
0232     s = s + testLineEdit->text();
0233     statusBar->showMessage(s);
0234 }
0235 void TestWindow::slotList(const QString &str)
0236 {
0237     QString s = QStringLiteral("You chose ");
0238     s = s + str;
0239     statusBar->showMessage(s);
0240 }
0241 
0242 void TestWindow::slotCompletion()
0243 {
0244     // Now do a completion
0245     // Call your completing function and set that text in lineedit
0246     // QString s = tb->getLinedText(/* ID */ 4)
0247     // QString completed = complete (s);
0248     // tb->setLinedText(/* ID */ 4, completed.data())
0249 
0250     // for now this:
0251 
0252     completions->popup(QCursor::pos()); // This popup should understunf keys up and down
0253 
0254     /* This is just an example. QLineEdit automatically sets cursor at end of string
0255      when ctrl-d or ctrl-s is pressed. KToolBar will also put cursor at end of text in LineEdit
0256      after inserting text with setLinedText (...).
0257     */
0258 }
0259 
0260 void TestWindow::slotListCompletion()
0261 {
0262     /*
0263      Combo is not behaving good and it is ugly. I will see how it behaves in Qt-1.3,
0264      and then decide should I make a new combobox.
0265      */
0266     QString s(testComboBox->currentText()); // get text in combo
0267     s += QStringLiteral("(completing)");
0268     // tb->getCombo(4)->changeItem(s.data());   // setTextIncombo
0269 }
0270 
0271 void TestWindow::slotCompletionsMenu(QAction *action)
0272 {
0273     // Now set text in lined
0274     QString s = action->text();
0275     testLineEdit->setText(s); // Cursor is automatically at the end of string after this
0276 }
0277 
0278 TestWindow::~TestWindow()
0279 {
0280     qDebug() << "kwindowtest finished";
0281 }
0282 
0283 void TestWindow::beFixed()
0284 {
0285     widget->setFixedSize(400, 200);
0286 }
0287 
0288 void TestWindow::beYFixed()
0289 {
0290     widget->setMinimumSize(400, 200);
0291     widget->setMaximumSize(9999, 200);
0292 }
0293 
0294 void TestWindow::slotImportant()
0295 {
0296     statusBar->showMessage(QStringLiteral("This important message will go away in 15 seconds"), 15000);
0297 }
0298 
0299 void TestWindow::slotExit()
0300 {
0301     if (exitB == true) {
0302         tb->removeAction(exitAction);
0303         exitB = false;
0304     } else {
0305         if (tb->actions().count() >= 7) {
0306             tb->insertAction(tb->actions().at(6), exitAction);
0307         } else {
0308             tb->addAction(exitAction);
0309         }
0310         exitB = true;
0311     }
0312 }
0313 
0314 void TestWindow::slotLined()
0315 {
0316     lineL = !lineL;
0317     testLineEdit->setEnabled(lineL); // enable/disable lined
0318 }
0319 
0320 void TestWindow::slotToggle(bool on)
0321 {
0322     if (on == true) {
0323         statusBar->showMessage(QStringLiteral("Toggle is on"));
0324     } else {
0325         statusBar->showMessage(QStringLiteral("Toggle is off"));
0326     }
0327 }
0328 
0329 void TestWindow::slotFrame()
0330 {
0331 #if 0
0332     if (greenF == false) {
0333         tb1->insertFrame(10, 100);
0334         tb1->alignItemRight(10);  // this is pointless 'cause tb1 is not fullwidth
0335 
0336         QFrame *myFrame = tb1->getFrame(10); // get frame pointer
0337 
0338         if (myFrame == 0) {
0339             warning("bad frame ID");
0340             return;
0341         }
0342 
0343         //paint it green
0344         // Or do whatever you want with it, just don't change its height (height = hardcoded = 24)
0345         // And don't move it
0346         // If you want to have something right from your toolbar you can reduce its
0347         // max_width with setMaxWidth()
0348         myFrame->setBackgroundColor(QColor("green"));
0349 
0350         greenF = true;
0351     } else {
0352         tb1->removeItem(10);
0353         greenF = false;
0354     }
0355 #endif
0356 }
0357 
0358 void TestWindow::slotMessage(int, bool boo)
0359 {
0360     if (boo) {
0361         statusBar->showMessage(QStringLiteral("This button does this and that"), 1500);
0362     } else {
0363         statusBar->clearMessage();
0364     }
0365 }
0366 // Now few Combo slots, for Torben
0367 
0368 void TestWindow::slotClearCombo()
0369 {
0370     testComboBox->clear();
0371 }
0372 
0373 void TestWindow::slotInsertListInCombo()
0374 {
0375     QStringList list;
0376     list.append(QStringLiteral("ListOne"));
0377     list.append(QStringLiteral("ListTwo"));
0378     list.append(QStringLiteral("ListThree"));
0379     list.append(QStringLiteral("ListFour"));
0380     list.append(QStringLiteral("ListFive"));
0381     list.append(QStringLiteral("ListSix"));
0382     list.append(QStringLiteral("ListSeven"));
0383     list.append(QStringLiteral("ListEight"));
0384     list.append(QStringLiteral("ListNine"));
0385     list.append(QStringLiteral("ListTen"));
0386     list.append(QStringLiteral("ListEleven"));
0387     list.append(QStringLiteral("ListAndSoOn"));
0388     testComboBox->addItems(list);
0389 }
0390 
0391 void TestWindow::slotMakeItem3Current()
0392 {
0393     testComboBox->setCurrentIndex(3);
0394 }
0395 
0396 int main(int argc, char *argv[])
0397 {
0398     QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
0399     QApplication::setApplicationName(QStringLiteral("kwindowtest"));
0400 
0401     QApplication myApp(argc, argv);
0402     TestWindow *test = new TestWindow;
0403 
0404     myApp.setQuitOnLastWindowClosed(false); // don't quit after the messagebox!
0405 
0406 #if 0
0407     int i = QMessageBox::information(0, "Select", "Select type of mainwidget",
0408                                      "Fixed", "Y-fixed", "Resizable");
0409     if (i == 0) {
0410         test->beFixed();
0411     } else if (i == 1) {
0412         test->beYFixed();
0413     }
0414 #endif
0415 
0416     test->show();
0417     myApp.setQuitOnLastWindowClosed(true);
0418     int ret = myApp.exec();
0419 
0420     // delete test;
0421     return ret;
0422 }
0423 
0424 #include "moc_kwindowtest.cpp"