strawberry-audio-player-win.../src/dialogs/console.cpp

89 lines
2.4 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
#include <QWidget>
#include <QDialog>
#include <QString>
#include <QStringList>
2018-02-27 18:06:05 +01:00
#include <QFont>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlError>
#include <QLineEdit>
#include <QPushButton>
#include <QScrollBar>
#include <QTextBrowser>
2018-02-27 18:06:05 +01:00
#include "console.h"
#include "core/logging.h"
2018-02-27 18:06:05 +01:00
#include "core/application.h"
#include "core/database.h"
2021-10-30 01:58:47 +02:00
Console::Console(Application *app, QWidget *parent) : QDialog(parent), ui_{}, app_(app) {
2018-10-02 00:38:52 +02:00
2018-02-27 18:06:05 +01:00
ui_.setupUi(this);
2022-03-22 21:09:05 +01:00
setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint);
2021-01-26 16:48:04 +01:00
QObject::connect(ui_.run, &QPushButton::clicked, this, &Console::RunQuery);
2018-02-27 18:06:05 +01:00
2024-04-09 23:20:26 +02:00
QFont font(QStringLiteral("Monospace"));
2018-02-27 18:06:05 +01:00
font.setStyleHint(QFont::TypeWriter);
ui_.output->setFont(font);
ui_.query->setFont(font);
}
void Console::RunQuery() {
QSqlDatabase db = app_->database()->Connect();
QSqlQuery query(db);
2023-07-21 07:12:20 +02:00
if (!query.prepare(ui_.query->text())) {
qLog(Error) << query.lastError();
return;
}
if (!query.exec()) {
qLog(Error) << query.lastError();
return;
}
2018-02-27 18:06:05 +01:00
ui_.output->append(QStringLiteral("<b>&gt; ") + query.executedQuery() + QStringLiteral("</b>"));
2018-02-27 18:06:05 +01:00
while (query.next() && query.isValid()) {
2018-02-27 18:06:05 +01:00
QSqlRecord record = query.record();
2021-06-20 19:04:08 +02:00
QStringList values; // clazy:exclude=container-inside-loop
values.reserve(record.count());
2018-02-27 18:06:05 +01:00
for (int i = 0; i < record.count(); ++i) {
values.append(record.value(i).toString());
}
2024-04-09 23:20:26 +02:00
ui_.output->append(values.join(QStringLiteral("|")));
2018-02-27 18:06:05 +01:00
}
ui_.output->verticalScrollBar()->setValue(ui_.output->verticalScrollBar()->maximum());
}