From 7c62abb3524f64d55a46892ff567fad9bc538531 Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 21 May 2017 17:59:27 +1200 Subject: [PATCH] working add items --- qbolt/mainwindow.cpp | 49 +++++++++++++++++++++++++++++--------------- qbolt/mainwindow.h | 1 + 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/qbolt/mainwindow.cpp b/qbolt/mainwindow.cpp index 9a743d8..7faa026 100644 --- a/qbolt/mainwindow.cpp +++ b/qbolt/mainwindow.cpp @@ -282,6 +282,29 @@ void MainWindow::on_actionClear_selection_triggered() } \ auto *bdb = GET_BDB(top); +void MainWindow::openEditor(BoltDB *bdb, QStringList saveAs, QByteArray saveAsKey, QByteArray currentContent) +{ + auto iw = new ItemWindow(); + iw->ContentArea()->setPlainText(QString::fromUtf8(currentContent)); + iw->setWindowTitle(QString::fromUtf8(saveAsKey)); + iw->setWindowModality(Qt::ApplicationModal); // we need this - otherwise we'll refresh a possibly-changed area after saving + connect(iw, &ItemWindow::finished, iw, [=](int exitCode){ + if (exitCode == ItemWindow::Accepted) { + QString err; + if (! bdb->setItem(saveAs, saveAsKey, iw->ContentArea()->toPlainText().toUtf8(), err)) { + QMessageBox qmb; + qmb.setText(tr("Error saving item content: %1").arg(err)); + qmb.exec(); + } + + refreshData(bdb, saveAs); + } + iw->deleteLater(); + }); + + iw->show(); +} + void MainWindow::on_bucketData_doubleClicked(const QModelIndex &index) { GET_ITM_TOP_BROWSE_BDB; @@ -297,22 +320,7 @@ void MainWindow::on_bucketData_doubleClicked(const QModelIndex &index) browse, key.toUtf8(), [=](QByteArray content) { - auto iw = new ItemWindow(); - iw->ContentArea()->setPlainText(QString::fromUtf8(content)); - iw->setWindowTitle(key); - connect(iw, &ItemWindow::finished, iw, [=](int exitCode){ - if (exitCode == ItemWindow::Accepted) { - QString err; - if (! bdb->setItem(browse, key.toUtf8(), iw->ContentArea()->toPlainText().toUtf8(), err)) { - QMessageBox qmb; - qmb.setText(tr("Error saving item content: %1").arg(err)); - qmb.exec(); - } - } - iw->deleteLater(); - }); - - iw->show(); + openEditor(bdb, browse, key.toUtf8(), content); }, [=](QString error) { QMessageBox qmb; @@ -378,7 +386,16 @@ void MainWindow::on_actionDelete_bucket_triggered() void MainWindow::on_AddDataButton_clicked() { + GET_ITM_TOP_BROWSE_BDB; + // Prompt for bucket name + + QString name = QInputDialog::getText(this, tr("New item"), tr("Enter a key for the new item:")); + if (name.length() == 0) { + return; + } + + openEditor(bdb, browse, name.toUtf8(), QByteArray()); } void MainWindow::on_DeleteDataButton_clicked() diff --git a/qbolt/mainwindow.h b/qbolt/mainwindow.h index df88fa5..cf44f55 100644 --- a/qbolt/mainwindow.h +++ b/qbolt/mainwindow.h @@ -55,6 +55,7 @@ protected: void openDatabase(QString file); void refreshBucketTree(QTreeWidgetItem* top); void refreshData(BoltDB *bdb, QStringList browse); + void openEditor(BoltDB *bdb, QStringList saveAs, QByteArray saveAsKey, QByteArray currentContent); private: Ui::MainWindow *ui;