File: kwindowtest.cpp

package info (click to toggle)
kf6-kxmlgui 6.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,708 kB
  • sloc: cpp: 16,748; xml: 45; python: 41; ansic: 22; sh: 18; makefile: 7
file content (423 lines) | stat: -rw-r--r-- 15,189 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include "kwindowtest.h"

#include <QDir>
#include <QMessageBox>
#include <QTest>
#include <QTextEdit>

#include <cstdlib>

#include <KActionMenu>
#include <KToggleAction>
#include <QAction>
#include <QActionGroup>
#include <QApplication>
#include <QComboBox>
#include <QLineEdit>
#include <kactioncollection.h>
#include <khelpmenu.h>
#include <kxmlguifactory.h>

TestWindow::TestWindow(QWidget *parent)
    : KXmlGuiWindow(parent)
{
    ena = false;
    exitB = true; // exit button is shown
    lineL = true; // LineEdit is enabled
    greenF = false; // Frame not inserted
    timer = nullptr;

    setCaption(QStringLiteral("test window"));

    // The xmlgui file defines the layout of the menus and toolbars.
    // We only need to create actions with the right name here.

    // First four  buttons
    fileNewAction = new QAction(QIcon::fromTheme(QStringLiteral("document-new")), QStringLiteral("Create.. (toggles upper button)"), this);
    actionCollection()->addAction(QStringLiteral("filenew"), fileNewAction);
    fileNewAction->setCheckable(true);
    connect(fileNewAction, &QAction::triggered, this, &TestWindow::slotNew);

    QAction *fileOpenAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open")), QStringLiteral("Open"), this);
    actionCollection()->addAction(QStringLiteral("fileopen"), fileOpenAction);
    connect(fileOpenAction, &QAction::triggered, this, &TestWindow::slotOpen);

    KActionMenu *fileFloppyAction = new KActionMenu(QIcon::fromTheme(QStringLiteral("filefloppy")), QStringLiteral("Save (beep or delayed popup)"), this);
    actionCollection()->addAction(QStringLiteral("filefloppy"), fileFloppyAction);
    connect(fileFloppyAction, &QAction::triggered, this, &TestWindow::slotSave);

    QAction *filePrintAction = new QAction(QIcon::fromTheme(QStringLiteral("document-print")), QStringLiteral("Print (enables/disables open)"), this);
    actionCollection()->addAction(QStringLiteral("fileprint"), filePrintAction);
    filePrintAction->setToolTip(QStringLiteral("This tooltip does not work for menu items"));
    filePrintAction->setWhatsThis(QStringLiteral("This is the longer explanation of the action"));
    filePrintAction->setStatusTip(QStringLiteral("This action is supposed to print, but in fact enables/disables open"));
    connect(filePrintAction, &QAction::triggered, this, &TestWindow::slotPrint);

    // And a combobox
    // arguments: text (or strList), ID, writable, signal, object, slot, enabled,
    //            tooltiptext, size
    testComboBox = new QComboBox(toolBar());
    // K3WidgetAction* comboAction = new K3WidgetAction(testComboBox, QString(), 0, 0, 0, actionCollection(), "combobox");
    // connect(testComboBox, SIGNAL(activated(QString)), this, SLOT(slotList(QString)));

    // Then one line editor
    // arguments: text, id, signal, object (this), slot, enabled, tooltiptext, size
    testLineEdit = new QLineEdit(toolBar());
    testLineEdit->setText(QStringLiteral("ftp://ftp.kde.org/pub/kde"));
    //    K3WidgetAction* lineEditAction = new K3WidgetAction(testLineEdit, QString(), 0, 0, 0, actionCollection(), "location");
    //    connect(testLineEdit, SIGNAL(returnPressed()), this, SLOT(slotReturn()));

    // Now add another button and align it right
    exitAction = new QAction(QIcon::fromTheme(QStringLiteral("application-exit")), QStringLiteral("Exit"), this);
    actionCollection()->addAction(QStringLiteral("exit"), exitAction);
    connect(exitAction, &QAction::triggered, qApp, &QCoreApplication::quit);

    // Another toolbar

    QAction *fileNewAction2 = new QAction(QIcon::fromTheme(QStringLiteral("document-new")), QStringLiteral("Create new file2 (Toggle)"), this);
    actionCollection()->addAction(QStringLiteral("filenew2"), fileNewAction2);
    connect(fileNewAction2, &QAction::toggled, this, &TestWindow::slotToggle);
    fileNewAction2->setToolTip(QStringLiteral("Tooltip"));
    fileNewAction2->setStatusTip(QStringLiteral("Statustip"));
    fileNewAction2->setWhatsThis(QStringLiteral("WhatsThis"));

    QAction *fileOpenAction2 = new QAction(QIcon::fromTheme(QStringLiteral("document-open")), QStringLiteral("Open (starts progress in sb)"), this);
    actionCollection()->addAction(QStringLiteral("fileopen2"), fileOpenAction2);
    connect(fileOpenAction2, &QAction::triggered, this, &TestWindow::slotOpen);
    fileOpenAction2->setToolTip(QStringLiteral("This action starts a progressbar inside the statusbar"));

    QAction *fileFloppyAction2 = new QAction(QIcon::fromTheme(QStringLiteral("filefloppy")), QStringLiteral("Save file2 (autorepeat)"), this);
    actionCollection()->addAction(QStringLiteral("filefloppy2"), fileFloppyAction2);
    connect(fileFloppyAction2, &QAction::triggered, this, &TestWindow::slotSave);

    itemsMenu = new QMenu;
    itemsMenu->addAction(QStringLiteral("delete/insert exit button"), this, &TestWindow::slotExit);
    itemsMenu->addAction(QStringLiteral("enable/disable lineedit"), this, &TestWindow::slotLined);
    itemsMenu->addAction(QStringLiteral("Toggle fileNew"), this, &TestWindow::slotNew);
    itemsMenu->addAction(QStringLiteral("Combo: clear"), this, &TestWindow::slotClearCombo);
    itemsMenu->addAction(QStringLiteral("Combo: insert list"), this, &TestWindow::slotInsertListInCombo);
    itemsMenu->addAction(QStringLiteral("Combo: make item 3 current"), this, &TestWindow::slotMakeItem3Current);
    itemsMenu->addAction(QStringLiteral("Important msg in statusbar"), this, &TestWindow::slotImportant);

    QAction *filePrintAction2 = new QAction(QIcon::fromTheme(QStringLiteral("document-print")), QStringLiteral("Print (pops menu)"), this);
    actionCollection()->addAction(QStringLiteral("fileprint2"), filePrintAction2);
    filePrintAction2->setMenu(itemsMenu);

    // *** RADIO BUTTONS
    QActionGroup *radioGroup = new QActionGroup(this);
    radioGroup->setExclusive(true);

    KToggleAction *radioButton1 = new KToggleAction(QIcon::fromTheme(QStringLiteral("document-new")), QStringLiteral("Radiobutton1"), this);
    actionCollection()->addAction(QStringLiteral("radioButton1"), radioButton1);
    radioButton1->setActionGroup(radioGroup);

    KToggleAction *radioButton2 = new KToggleAction(QIcon::fromTheme(QStringLiteral("document-open")), QStringLiteral("Radiobutton2"), this);
    actionCollection()->addAction(QStringLiteral("radioButton2"), radioButton2);
    radioButton2->setActionGroup(radioGroup);

    KToggleAction *radioButton3 = new KToggleAction(QIcon::fromTheme(QStringLiteral("filefloppy")), QStringLiteral("Radiobutton3"), this);
    actionCollection()->addAction(QStringLiteral("radioButton3"), radioButton3);
    radioButton3->setActionGroup(radioGroup);

    KToggleAction *radioButton4 = new KToggleAction(QIcon::fromTheme(QStringLiteral("document-print")), QStringLiteral("Radiobutton4"), this);
    actionCollection()->addAction(QStringLiteral("radioButton4"), radioButton4);
    radioButton4->setActionGroup(radioGroup);

    connect(radioGroup, &QActionGroup::triggered, this, &TestWindow::slotToggled);

    /*!************************************************/
    /*Now, we setup statusbar; order is not important. */
    /*!************************************************/
    statusBar = new QStatusBar(this);
    // statusBar->insertItem("Hi there!                         ", 0);
    // statusBar->insertItem("Look for tooltips to see functions", 1);
    setStatusBar(statusBar);

    // DigitalClock *clk = new DigitalClock (statusBar);
    // clk->setFrameStyle(QFrame::NoFrame);
    // statusBar->insertWidget(clk, 70, 2);

    // Set main widget. In this example it is Qt's multiline text editor.
    widget = new QTextEdit(this);
    setCentralWidget(widget);

    // Setup is now complete

    setAutoSaveSettings();

    // This is not strictly related to toolbars, menubars or KMainWindow.
    // Setup popup for completions
    completions = new QMenu;

    completions->addAction(QStringLiteral("/"));
    completions->addAction(QStringLiteral("/usr/"));
    completions->addAction(QStringLiteral("/lib/"));
    completions->addAction(QStringLiteral("/var/"));
    completions->addAction(QStringLiteral("/bin/"));
    completions->addAction(QStringLiteral("/kde/"));
    completions->addAction(QStringLiteral("/home/"));
    completions->addAction(QStringLiteral("/vmlinuz :-)"));

    connect(completions, &QMenu::triggered, this, &TestWindow::slotCompletionsMenu);
    pr = nullptr;

    // KXMLGUIClient looks in the "data" resource for the .rc files
    // This line is for test programs only!
    qputenv("XDG_DATA_HOME", QFile::encodeName(QFileInfo(QFINDTESTDATA("kwindowtest.rc")).absolutePath()));
    setupGUI(QSize(400, 500), Default, QStringLiteral("kwindowtest.rc"));

    tb = toolBar();
    tb1 = toolBar(QStringLiteral("AnotherToolBar"));
}
/*!*********************************/
/*  Now slots for toolbar actions  */
/*!*********************************/
void TestWindow::slotToggled(QAction *)
{
    statusBar->showMessage(QStringLiteral("Button toggled"), 1500);
}

void TestWindow::slotInsertClock()
{
    // DigitalClock *clock = new DigitalClock(tb1);
    // clock->setFrameStyle(QFrame::NoFrame);
    // tb1->insertWidget(8, 70, clock);
}

void TestWindow::slotNew()
{
    // tb1->actions()[0]->toggle();
    // toolBar()->removeAction( fileNewAction );
}
void TestWindow::slotOpen()
{
    if (pr == nullptr) {
        pr = new QProgressBar(statusBar);
        pr->show();
    }
    //  statusBar->message(pr);
    if (!timer) {
        timer = new QTimer(this);
        connect(timer, &QTimer::timeout, this, &TestWindow::slotGoGoGoo);
    }
    timer->start(100);
}

void TestWindow::slotGoGoGoo()
{
    pr->setValue(pr->value() + 1);
    if (pr->value() == 100) {
        timer->stop();
        statusBar->clearMessage();
        delete pr;
        pr = nullptr;
    }
}

void TestWindow::slotSave()
{
    qApp->beep();
    statusBar->showMessage(QStringLiteral("Saving properties…"));
}

void TestWindow::slotPrint()
{
    statusBar->showMessage(QStringLiteral("Print file pressed"));
    ena = !ena;
    qobject_cast<QAction *>(sender())->setEnabled(ena);
}
void TestWindow::slotReturn()
{
    QString s = QStringLiteral("You entered ");
    s = s + testLineEdit->text();
    statusBar->showMessage(s);
}
void TestWindow::slotList(const QString &str)
{
    QString s = QStringLiteral("You chose ");
    s = s + str;
    statusBar->showMessage(s);
}

void TestWindow::slotCompletion()
{
    // Now do a completion
    // Call your completing function and set that text in lineedit
    // QString s = tb->getLinedText(/* ID */ 4)
    // QString completed = complete (s);
    // tb->setLinedText(/* ID */ 4, completed.data())

    // for now this:

    completions->popup(QCursor::pos()); // This popup should understunf keys up and down

    /* This is just an example. QLineEdit automatically sets cursor at end of string
     when ctrl-d or ctrl-s is pressed. KToolBar will also put cursor at end of text in LineEdit
     after inserting text with setLinedText (...).
    */
}

void TestWindow::slotListCompletion()
{
    /*
     Combo is not behaving good and it is ugly. I will see how it behaves in Qt-1.3,
     and then decide should I make a new combobox.
     */
    QString s(testComboBox->currentText()); // get text in combo
    s += QStringLiteral("(completing)");
    // tb->getCombo(4)->changeItem(s.data());   // setTextIncombo
}

void TestWindow::slotCompletionsMenu(QAction *action)
{
    // Now set text in lined
    QString s = action->text();
    testLineEdit->setText(s); // Cursor is automatically at the end of string after this
}

TestWindow::~TestWindow()
{
    qDebug() << "kwindowtest finished";
}

void TestWindow::beFixed()
{
    widget->setFixedSize(400, 200);
}

void TestWindow::beYFixed()
{
    widget->setMinimumSize(400, 200);
    widget->setMaximumSize(9999, 200);
}

void TestWindow::slotImportant()
{
    statusBar->showMessage(QStringLiteral("This important message will go away in 15 seconds"), 15000);
}

void TestWindow::slotExit()
{
    if (exitB == true) {
        tb->removeAction(exitAction);
        exitB = false;
    } else {
        if (tb->actions().count() >= 7) {
            tb->insertAction(tb->actions().at(6), exitAction);
        } else {
            tb->addAction(exitAction);
        }
        exitB = true;
    }
}

void TestWindow::slotLined()
{
    lineL = !lineL;
    testLineEdit->setEnabled(lineL); // enable/disable lined
}

void TestWindow::slotToggle(bool on)
{
    if (on == true) {
        statusBar->showMessage(QStringLiteral("Toggle is on"));
    } else {
        statusBar->showMessage(QStringLiteral("Toggle is off"));
    }
}

void TestWindow::slotFrame()
{
#if 0
    if (greenF == false) {
        tb1->insertFrame(10, 100);
        tb1->alignItemRight(10);  // this is pointless 'cause tb1 is not fullwidth

        QFrame *myFrame = tb1->getFrame(10); // get frame pointer

        if (myFrame == 0) {
            warning("bad frame ID");
            return;
        }

        //paint it green
        // Or do whatever you want with it, just don't change its height (height = hardcoded = 24)
        // And don't move it
        // If you want to have something right from your toolbar you can reduce its
        // max_width with setMaxWidth()
        myFrame->setBackgroundColor(QColor("green"));

        greenF = true;
    } else {
        tb1->removeItem(10);
        greenF = false;
    }
#endif
}

void TestWindow::slotMessage(int, bool boo)
{
    if (boo) {
        statusBar->showMessage(QStringLiteral("This button does this and that"), 1500);
    } else {
        statusBar->clearMessage();
    }
}
// Now few Combo slots, for Torben

void TestWindow::slotClearCombo()
{
    testComboBox->clear();
}

void TestWindow::slotInsertListInCombo()
{
    QStringList list;
    list.append(QStringLiteral("ListOne"));
    list.append(QStringLiteral("ListTwo"));
    list.append(QStringLiteral("ListThree"));
    list.append(QStringLiteral("ListFour"));
    list.append(QStringLiteral("ListFive"));
    list.append(QStringLiteral("ListSix"));
    list.append(QStringLiteral("ListSeven"));
    list.append(QStringLiteral("ListEight"));
    list.append(QStringLiteral("ListNine"));
    list.append(QStringLiteral("ListTen"));
    list.append(QStringLiteral("ListEleven"));
    list.append(QStringLiteral("ListAndSoOn"));
    testComboBox->addItems(list);
}

void TestWindow::slotMakeItem3Current()
{
    testComboBox->setCurrentIndex(3);
}

int main(int argc, char *argv[])
{
    QApplication::setApplicationName(QStringLiteral("kwindowtest"));

    QApplication myApp(argc, argv);
    TestWindow *test = new TestWindow;

    myApp.setQuitOnLastWindowClosed(false); // don't quit after the messagebox!

#if 0
    int i = QMessageBox::information(0, "Select", "Select type of mainwidget",
                                     "Fixed", "Y-fixed", "Resizable");
    if (i == 0) {
        test->beFixed();
    } else if (i == 1) {
        test->beYFixed();
    }
#endif

    test->show();
    myApp.setQuitOnLastWindowClosed(true);
    int ret = myApp.exec();

    // delete test;
    return ret;
}

#include "moc_kwindowtest.cpp"