Fix correct signal/slot
[kdepim.git] / akonadiconsole / jobtrackerwidget.cpp
blob45c26cf15187328b16299ac06ce1ab8d0520a1f0
1 /*
2 This file is part of Akonadi.
4 Copyright (c) 2009 Till Adam <adam@kde.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 USA.
22 #include "jobtrackerwidget.h"
23 #include <QCheckBox>
25 #include "jobtrackermodel.h"
27 #include <AkonadiWidgets/controlgui.h>
29 #include <KLocalizedString>
30 #include <QUrl>
32 #include <QTreeView>
33 #include <QHeaderView>
34 #include <QVBoxLayout>
35 #include <QMenu>
36 #include <QPushButton>
37 #include <QFile>
38 #include <QFileDialog>
40 class JobTrackerWidget::Private
42 public:
43 JobTrackerModel *model;
46 JobTrackerWidget::JobTrackerWidget(const char *name, QWidget *parent, const QString &checkboxText)
47 : QWidget(parent),
48 d(new Private)
50 d->model = new JobTrackerModel(name, this);
52 QVBoxLayout *layout = new QVBoxLayout(this);
54 QCheckBox *enableCB = new QCheckBox(this);
55 enableCB->setText(checkboxText);
56 connect(enableCB, &QAbstractButton::toggled, d->model, &JobTrackerModel::setEnabled);
57 layout->addWidget(enableCB);
59 QTreeView *tv = new QTreeView(this);
60 tv->setModel(d->model);
61 tv->expandAll();
62 tv->setAlternatingRowColors(true);
63 tv->setContextMenuPolicy(Qt::CustomContextMenu);
64 // too slow with many jobs:
65 // tv->header()->setResizeMode( QHeaderView::ResizeToContents );
66 connect(d->model, &JobTrackerModel::modelReset, tv, &QTreeView::expandAll);
67 connect(tv, &QTreeView::customContextMenuRequested, this, &JobTrackerWidget::contextMenu);
68 layout->addWidget(tv);
69 d->model->setEnabled(false); // since it can be slow, default to off
71 QHBoxLayout *layout2 = new QHBoxLayout;
72 QPushButton *button = new QPushButton(QStringLiteral("Save to file..."), this);
73 connect(button, &QAbstractButton::clicked,
74 this, &JobTrackerWidget::slotSaveToFile);
75 layout2->addWidget(button);
76 layout2->addStretch(1);
77 layout->addLayout(layout2);
79 Akonadi::ControlGui::widgetNeedsAkonadi(this);
82 JobTrackerWidget::~JobTrackerWidget()
84 delete d;
87 void JobTrackerWidget::contextMenu(const QPoint &/*pos*/)
89 QMenu menu;
90 menu.addAction(QStringLiteral("Clear View"), d->model, SLOT(resetTracker()));
91 menu.exec(QCursor::pos());
94 void JobTrackerWidget::slotSaveToFile()
96 const QString fileName = QFileDialog::getSaveFileName(this, QString(), QString(), QString());
97 if (fileName.isEmpty()) {
98 return;
101 QFile file(fileName);
102 if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
103 return;
106 file.write("Job ID\t\tCreated\t\tWait Time\tJob Duration\tJob Type\t\tState\tInfo\n");
108 writeRows(QModelIndex(), file, 0);
110 file.close();
113 void JobTrackerWidget::writeRows(const QModelIndex &parent, QFile &file, int indentLevel)
115 for (int row = 0; row < d->model->rowCount(parent); ++row) {
116 QByteArray data;
117 for (int tabs = 0; tabs < indentLevel; ++tabs) {
118 data += '\t';
120 const int columnCount = d->model->columnCount(parent);
121 for (int column = 0; column < columnCount; ++column) {
122 const QModelIndex index = d->model->index(row, column, parent);
123 data += index.data().toByteArray();
124 if (column < columnCount - 1) {
125 data += '\t';
128 data += '\n';
129 file.write(data);
131 const QModelIndex index = d->model->index(row, 0, parent);
132 writeRows(index, file, indentLevel + 1);