File indexing completed on 2024-05-19 05:35:23

0001 //////////////////////////////////////////////////////////////////////////////
0002 // oxygenbenchmarkwidget.cpp
0003 // oxygen buttons demo widget
0004 // -------------------
0005 //
0006 // SPDX-FileCopyrightText: 2010 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0007 //
0008 // SPDX-License-Identifier: MIT
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #include "oxygenbenchmarkwidget.h"
0012 
0013 #include <QAbstractItemView>
0014 #include <QIcon>
0015 
0016 namespace Oxygen
0017 {
0018 //_______________________________________________
0019 BenchmarkWidget::BenchmarkWidget(QWidget *parent)
0020     : DemoWidget(parent)
0021 {
0022     // setup ui
0023     ui.setupUi(this);
0024     ui.runButton->setIcon(QIcon::fromTheme(QStringLiteral("system-run")));
0025     ui.grabMouseCheckBox->setChecked(Simulator::grabMouse());
0026     connect(ui.grabMouseCheckBox, SIGNAL(toggled(bool)), SLOT(updateGrabMouse(bool)));
0027     connect(ui.runButton, SIGNAL(clicked()), SLOT(run()));
0028 }
0029 
0030 //_______________________________________________
0031 void BenchmarkWidget::init(KPageDialog *dialog, QVector<KPageWidgetItem *> items)
0032 {
0033     _pageDialog = dialog;
0034 
0035     for (auto &&item : items) {
0036         // get header and widget
0037         auto header = item->header();
0038         auto demoWidget(qobject_cast<DemoWidget *>(item->widget()));
0039         if (!demoWidget)
0040             continue;
0041 
0042         // do not add oneself to the list
0043         if (qobject_cast<BenchmarkWidget *>(demoWidget))
0044             continue;
0045 
0046         // add checkbox
0047         QCheckBox *checkbox(new QCheckBox(this));
0048         checkbox->setText(header);
0049 
0050         const bool hasBenchmark(demoWidget->metaObject()->indexOfSlot("benchmark()") >= 0);
0051         checkbox->setEnabled(hasBenchmark);
0052         checkbox->setChecked(hasBenchmark);
0053 
0054         if (hasBenchmark) {
0055             connect(this, SIGNAL(runBenchmark()), demoWidget, SLOT(benchmark()));
0056         }
0057 
0058         ui.verticalLayout->addWidget(checkbox);
0059 
0060         _items.append(ItemPair(checkbox, item));
0061 
0062         connect(checkbox, SIGNAL(toggled(bool)), SLOT(updateButtonState()));
0063     }
0064 }
0065 
0066 //_______________________________________________
0067 void BenchmarkWidget::updateButtonState(void)
0068 {
0069     bool enabled(false);
0070     for (auto &&item : _items) {
0071         if (item.first->isEnabled() && item.first->isChecked()) {
0072             enabled = true;
0073             break;
0074         }
0075     }
0076 
0077     ui.runButton->setEnabled(enabled);
0078 }
0079 
0080 //_______________________________________________
0081 void BenchmarkWidget::run(void)
0082 {
0083     // disable button and groupbox
0084     ui.runButton->setEnabled(false);
0085     Simulator::setGrabMouse(ui.grabMouseCheckBox->isChecked());
0086     for (int index = 0; index < _items.size(); ++index) {
0087         auto item(_items[index]);
0088 
0089         // check state
0090         if (!(item.first->isEnabled() && item.first->isChecked())) {
0091             continue;
0092         }
0093 
0094         if (simulator().aborted())
0095             return;
0096         else {
0097             selectPage(index);
0098             emit runBenchmark();
0099         }
0100     }
0101 
0102     // re-select last page
0103     selectPage(_items.size());
0104 
0105     // disable button and groupbox
0106     ui.runButton->setEnabled(true);
0107 }
0108 
0109 //_______________________________________________
0110 void BenchmarkWidget::selectPage(int index) const
0111 {
0112     // check dialog
0113     if (!_pageDialog)
0114         return;
0115 
0116     // try find item view from pageView
0117     auto view(_pageDialog.data()->findChild<QAbstractItemView *>());
0118 
0119     // select in list
0120     if (view) {
0121         simulator().selectItem(view, index);
0122         simulator().run();
0123     }
0124 
0125     return;
0126 }
0127 }