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

94 lines
2.5 KiB
C++
Raw Permalink 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"
2024-10-22 18:12:33 +02:00
#include "includes/shared_ptr.h"
#include "core/logging.h"
2018-02-27 18:06:05 +01:00
#include "core/database.h"
2024-10-20 06:38:55 +02:00
using namespace Qt::Literals::StringLiterals;
2024-10-22 18:12:33 +02:00
Console::Console(const SharedPtr<Database> database, QWidget *parent) : QDialog(parent), ui_{}, database_(database) {
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-10-20 06:38:55 +02:00
QFont font(u"Monospace"_s);
2018-02-27 18:06:05 +01:00
font.setStyleHint(QFont::TypeWriter);
ui_.output->setFont(font);
ui_.query->setFont(font);
}
void Console::RunQuery() {
2024-10-22 18:12:33 +02:00
QSqlDatabase db = database_->Connect();
QSqlQuery query(db);
2023-07-21 07:12:20 +02:00
if (!query.prepare(ui_.query->text())) {
qLog(Error) << query.lastError();
2024-10-22 18:12:33 +02:00
Q_EMIT Error(query.lastError().text());
2023-07-21 07:12:20 +02:00
return;
}
if (!query.exec()) {
qLog(Error) << query.lastError();
2024-10-22 18:12:33 +02:00
Q_EMIT Error(query.lastError().text());
return;
}
2018-02-27 18:06:05 +01:00
2024-10-20 06:38:55 +02:00
ui_.output->append(u"<b>&gt; "_s + query.executedQuery() + u"</b>"_s);
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());
}
ui_.output->append(values.join(u'|'));
2018-02-27 18:06:05 +01:00
}
ui_.output->verticalScrollBar()->setValue(ui_.output->verticalScrollBar()->maximum());
}