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,
22 #include "jobtrackerwidget.h"
25 #include "jobtrackermodel.h"
27 #include <AkonadiWidgets/controlgui.h>
29 #include <KLocalizedString>
33 #include <QHeaderView>
34 #include <QVBoxLayout>
36 #include <QPushButton>
38 #include <QFileDialog>
40 class JobTrackerWidget::Private
43 JobTrackerModel
*model
;
46 JobTrackerWidget::JobTrackerWidget(const char *name
, QWidget
*parent
, const QString
&checkboxText
)
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
);
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()
87 void JobTrackerWidget::contextMenu(const QPoint
&/*pos*/)
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()) {
101 QFile
file(fileName
);
102 if (!file
.open(QIODevice::WriteOnly
| QIODevice::Truncate
)) {
106 file
.write("Job ID\t\tCreated\t\tWait Time\tJob Duration\tJob Type\t\tState\tInfo\n");
108 writeRows(QModelIndex(), file
, 0);
113 void JobTrackerWidget::writeRows(const QModelIndex
&parent
, QFile
&file
, int indentLevel
)
115 for (int row
= 0; row
< d
->model
->rowCount(parent
); ++row
) {
117 for (int tabs
= 0; tabs
< indentLevel
; ++tabs
) {
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) {
131 const QModelIndex index
= d
->model
->index(row
, 0, parent
);
132 writeRows(index
, file
, indentLevel
+ 1);