qbolt/qbolt/mainwindow.cpp

239 lines
5.9 KiB
C++
Raw Normal View History

2017-05-16 07:34:54 +00:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "boltdb.h"
2017-05-16 07:34:54 +00:00
#include <QFileDialog>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
on_bucketTree_currentItemChanged(nullptr, nullptr);
databaseContext = new QMenu();
databaseContext->addAction(ui->actionRefresh_buckets);
databaseContext->addAction(ui->actionDisconnect);
bucketContext = new QMenu();
bucketContext->addAction(ui->actionDelete_bucket);
2017-05-16 07:34:54 +00:00
}
MainWindow::~MainWindow()
{
delete ui;
}
static const int BdbPointerRole = Qt::UserRole + 1;
#define SET_BDB(top, bdb) top->setData(0, BdbPointerRole, QVariant::fromValue<void*>(static_cast<void*>(bdb)))
#define GET_BDB(top) static_cast<BoltDB*>( top->data(0, BdbPointerRole).value<void*>() )
2017-05-21 00:39:55 +00:00
/*
static BoltDB* GET_BDB(QTreeWidgetItem *top) {
while(top->parent() != nullptr) {
top = top->parent();
}
return static_cast<BoltDB*>( top->data(0, BdbPointerRole).value<void*>() );
}
*/
2017-05-16 07:34:54 +00:00
void MainWindow::on_actionOpen_database_triggered()
{
QString file = QFileDialog::getOpenFileName(this, tr("Select bolt database..."));
if (! file.length()) {
return;
}
// Open
QString error;
auto *bdb = BoltDB::createFrom(file, error);
if (bdb == nullptr) {
2017-05-16 07:34:54 +00:00
QMessageBox qmb;
qmb.setText(tr("Error opening database: %1").arg(error));
2017-05-16 07:34:54 +00:00
qmb.exec();
return;
}
2017-05-20 03:47:05 +00:00
QTreeWidgetItem *top = new QTreeWidgetItem();
top->setText(0, QFileInfo(file).fileName());
2017-05-20 03:47:05 +00:00
top->setIcon(0, QIcon(":/rsrc/database.png"));
SET_BDB(top, bdb);
2017-05-20 03:47:05 +00:00
ui->bucketTree->addTopLevelItem(top);
refreshBucketTree(top);
ui->bucketTree->setCurrentItem(top);
}
void MainWindow::refreshBucketTree(QTreeWidgetItem* itm)
{
// ui->bucketTree->clearSelection();
QTreeWidgetItem *top = itm;
QStringList browsePath;
while(top->parent() != nullptr) {
browsePath.push_front(top->text(0));
top = top->parent();
}
// Remove existing children
for (int i = itm->childCount(); i --> 0;) {
delete itm->takeChild(i);
}
if (browsePath.size() > 1) {
return; // only go one level deep
}
auto *bdb = GET_BDB(top);
QString error;
bool ok = bdb->listBuckets(
browsePath,
error,
[=](QByteArray qba){
QTreeWidgetItem *child = new QTreeWidgetItem();
child->setText(0, QString::fromUtf8(qba));
child->setIcon(0, QIcon(":/rsrc/table.png"));
itm->addChild(child);
refreshBucketTree(child);
}
);
if (!ok) {
QMessageBox qmb;
qmb.setText(tr("Error listing buckets: %1").arg(error));
qmb.exec();
// (continue)
}
2017-05-16 07:34:54 +00:00
}
void MainWindow::on_actionExit_triggered()
{
close();
}
void MainWindow::on_actionAbout_Qt_triggered()
{
QApplication::aboutQt();
}
void MainWindow::on_actionAbout_qbolt_triggered()
{
QMessageBox::about(
this,
QApplication::applicationDisplayName(),
"<b>QBolt</b><br>Graphical interface for managing Bolt databases<br><br>"
"- <a href='https://github.com/boltdb/bolt'>About BoltDB</a><br>"
"- <a href='http://www.famfamfam.com/lab/icons/silk/'>FamFamFam &quot;Silk&quot; icon set</a><br>"
"- <a href='https://code.ivysaur.me/qbolt'>QBolt homepage</a><br>"
);
}
void MainWindow::on_actionDisconnect_triggered()
{
QTreeWidgetItem *top = lastContextSelection;
if (top->parent()) {
return; // somehow we didn't select a top-level item
}
auto *bdb = GET_BDB(top);
// Remove UI
ui->bucketTree->clearSelection();
delete top;
// Disconnect from DB
delete bdb;
}
void MainWindow::on_bucketTree_customContextMenuRequested(const QPoint &pos)
{
auto *itm = ui->bucketTree->itemAt(pos);
if (itm == nullptr) {
return;
}
lastContextSelection = itm;
if (itm->parent() != nullptr) {
// Child item, show the bucket menu
bucketContext->popup(ui->bucketTree->mapToGlobal(pos));
} else {
// Top-level item, show the database menu
databaseContext->popup(ui->bucketTree->mapToGlobal(pos));
}
}
void MainWindow::on_actionRefresh_buckets_triggered()
{
}
void MainWindow::on_bucketTree_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
Q_UNUSED(previous);
if (current == nullptr) {
2017-05-21 00:39:55 +00:00
ui->stackedWidget->setVisible(false);
return;
}
2017-05-21 00:39:55 +00:00
ui->stackedWidget->setVisible(true);
2017-05-20 23:59:38 +00:00
if (current->parent() == nullptr) {
// Selected a database
2017-05-21 00:39:55 +00:00
ui->stackedWidget->setCurrentWidget(ui->databasePage);
2017-05-20 23:59:38 +00:00
auto *bdb = GET_BDB(current);
2017-05-21 00:39:55 +00:00
bdb->getStatsJSON(
[=](QString j) {
ui->databasePropertiesArea->clear();
ui->databasePropertiesArea->setPlainText(j);
},
[=](QString error) {
QMessageBox qmb;
qmb.setText(tr("Error retrieving database statistics: %1").arg(error));
qmb.exec();
}
);
2017-05-20 23:59:38 +00:00
} else {
// Selected a bucket
2017-05-21 00:39:55 +00:00
ui->stackedWidget->setCurrentWidget(ui->bucketPage);
QStringList browse;
QTreeWidgetItem *top = current;
while (top->parent() != nullptr) {
browse.push_front(top->text(0));
top = top->parent();
}
auto *bdb = GET_BDB(top);
bdb->getBucketStatsJSON(
browse,
[=](QString j) {
ui->bucketPropertiesArea->clear();
ui->bucketPropertiesArea->setPlainText(j);
},
[=](QString error) {
QMessageBox qmb;
qmb.setText(tr("Error retrieving bucket statistics: %1").arg(error));
qmb.exec();
}
);
2017-05-20 23:59:38 +00:00
// Load the data tab
// TODO
}
}
void MainWindow::on_actionClear_selection_triggered()
{
ui->bucketTree->setCurrentItem(nullptr);
}