Astyle kdelibs
[kdepim.git] / akonadiconsole / dbconsole.cpp
blobe4c1bc469e8d7c3a02147290b0cbc5421555b37c
1 /*
2 Copyright (c) 2009 Volker Krause <vkrause@kde.org>
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
20 #include "dbconsole.h"
21 #include "dbaccess.h"
23 #include <QAction>
25 #include <KStandardAction>
26 #include <KSharedConfig>
27 #include <KConfigGroup>
28 #include <QIcon>
30 #include <QApplication>
31 #include <QClipboard>
32 #include <QSqlQueryModel>
33 #include <QSqlError>
34 #include <QFontDatabase>
36 DbConsole::DbConsole(QWidget *parent) :
37 QWidget(parent),
38 mQueryModel(Q_NULLPTR)
40 ui.setupUi(this);
42 QAction *copyAction = KStandardAction::copy(this, SLOT(copyCell()), this);
43 ui.resultView->addAction(copyAction);
45 ui.execButton->setIcon(QIcon::fromTheme(QStringLiteral("application-x-executable")));
46 ui.execButton->setShortcut(Qt::CTRL + Qt::Key_Return);
47 connect(ui.execButton, &QPushButton::clicked, this, &DbConsole::execClicked);
49 ui.queryEdit->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
50 ui.errorView->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
52 ui.queryEdit->setPlainText(KSharedConfig::openConfig()->group("DBConsole").readEntry("queryText"));
55 void DbConsole::execClicked()
57 const QString query = ui.queryEdit->toPlainText();
58 if (query.isEmpty()) {
59 return;
61 delete mQueryModel;
62 mQueryModel = new QSqlQueryModel(this);
63 mQueryModel->setQuery(query, DbAccess::database());
64 ui.resultView->setModel(mQueryModel);
66 if (mQueryModel->lastError().isValid()) {
67 ui.errorView->appendPlainText(mQueryModel->lastError().text());
68 ui.resultStack->setCurrentWidget(ui.errorViewPage);
69 } else {
70 ui.errorView->clear();
71 ui.resultStack->setCurrentWidget(ui.resultViewPage);
74 KSharedConfig::openConfig()->group("DBConsole").writeEntry("queryText", query);
77 void DbConsole::copyCell()
79 QModelIndex index = ui.resultView->currentIndex();
80 if (!index.isValid()) {
81 return;
83 QString text = index.data(Qt::DisplayRole).toString();
84 QApplication::clipboard()->setText(text);