qbolt/qbolt/mainwindow.cpp

167 lines
4.1 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "boltdb.h"
#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);
}
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*>() )
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) {
QMessageBox qmb;
qmb.setText(tr("Error opening database: %1").arg(error));
qmb.exec();
return;
}
QTreeWidgetItem *top = new QTreeWidgetItem();
top->setText(0, QFileInfo(file).fileName());
top->setIcon(0, QIcon(":/rsrc/database.png"));
SET_BDB(top, bdb);
ui->bucketTree->addTopLevelItem(top);
refreshBucketTree(top);
}
void MainWindow::refreshBucketTree(QTreeWidgetItem* top)
{
ui->bucketTree->clearSelection();
//top->setSelected(true);
ui->bucketTree->setCurrentItem(top);
auto *bdb = GET_BDB(top);
for (int i = top->childCount(); i --> 0;) {
delete top->takeChild(i);
}
QString error;
bool ok = bdb->listBucketsAtRoot(error, [=](QByteArray qba){
QTreeWidgetItem *child = new QTreeWidgetItem();
child->setText(0, QString::fromUtf8(qba));
child->setIcon(0, QIcon(":/rsrc/table.png"));
top->addChild(child);
});
if (!ok) {
QMessageBox qmb;
qmb.setText(tr("Error listing buckets: %1").arg(error));
qmb.exec();
// (continue)
}
}
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) {
ui->tabWidget->setVisible(false);
return;
}
ui->tabWidget->setVisible(true);
// refresh properties for the currently selected tree item...
}
void MainWindow::on_actionClear_selection_triggered()
{
ui->bucketTree->setCurrentItem(nullptr);
}