diff --git a/src/EditDialog.cpp b/src/EditDialog.cpp index e99359a7..42faabd2 100644 --- a/src/EditDialog.cpp +++ b/src/EditDialog.cpp @@ -11,10 +11,12 @@ #include #include //#include +#include EditDialog::EditDialog(QWidget* parent) : QDialog(parent), ui(new Ui::EditDialog), + currentIndex(QModelIndex()), dataType(Null) { ui->setupUi(this); @@ -30,7 +32,11 @@ EditDialog::EditDialog(QWidget* parent) QShortcut* ins = new QShortcut(QKeySequence(Qt::Key_Insert), this); connect(ins, SIGNAL(activated()), this, SLOT(toggleOverwriteMode())); - reset(); + // Set the font for the text and hex editors + QFont editorFont(PreferencesDialog::getSettingsValue("databrowser", "font").toString()); + editorFont.setPointSize(PreferencesDialog::getSettingsValue("databrowser", "fontsize").toInt()); + ui->editorText->setFont(editorFont); + hexEdit->setFont(editorFont); } EditDialog::~EditDialog() @@ -38,30 +44,13 @@ EditDialog::~EditDialog() delete ui; } -void EditDialog::reset() +void EditDialog::setCurrentIndex(const QModelIndex& idx) { - // Set the font for the text and hex editors - QFont editorFont(PreferencesDialog::getSettingsValue("databrowser", "font").toString()); - editorFont.setPointSize(PreferencesDialog::getSettingsValue("databrowser", "fontsize").toInt()); - ui->editorText->setFont(editorFont); - hexEdit->setFont(editorFont); + currentIndex = QPersistentModelIndex(idx); - curRow = -1; - curCol = -1; - ui->editorText->clear(); - ui->editorText->setFocus(); - ui->editorImage->clear(); - hexEdit->setData(QByteArray()); - oldData = ""; - dataType = Null; - - // Update the cell data info in the bottom left of the Edit Cell - updateCellInfo(hexEdit->data()); -} - -void EditDialog::closeEvent(QCloseEvent*) -{ - emit goingAway(); + QByteArray data = idx.data(Qt::EditRole).toByteArray(); + loadData(data); + updateCellInfo(data); } void EditDialog::showEvent(QShowEvent*) @@ -143,6 +132,7 @@ void EditDialog::loadData(const QByteArray& data) // The text widget buffer is now the main data source dataSource = TextBuffer; + break; case HexEditor: @@ -169,6 +159,7 @@ void EditDialog::loadData(const QByteArray& data) // The text widget buffer is now the main data source dataSource = TextBuffer; + break; } break; @@ -229,24 +220,6 @@ void EditDialog::loadData(const QByteArray& data) } } -// Loads data from a cell into the Edit Cell window -void EditDialog::loadDataFromCell(const QByteArray& data, int row, int col) -{ - // Store the position of the being edited - curRow = row; - curCol = col; - - // Store a copy of the data, so we can check if it has changed when the - // accept() method is called - oldData = data; - - // Load the data into the cell - loadData(data); - - // Update the cell data info in the bottom left of the Edit Cell - updateCellInfo(data); -} - void EditDialog::importData() { // Get list of supported image file formats to include them in the file dialog filter @@ -328,21 +301,21 @@ void EditDialog::setNull() void EditDialog::accept() { - // Check if the current cell data is different from the original cell data + if(!currentIndex.isValid()) + return; + if (dataSource == TextBuffer) { + QString oldData = currentIndex.data(Qt::EditRole).toString(); QString newData = ui->editorText->toPlainText(); - if ((newData != oldData) || (newData.isNull() != oldData.isNull())) { + if (oldData != newData) // The data is different, so commit it back to the database - QByteArray convertedText = newData.toUtf8(); - emit recordTextUpdated(curRow, curCol, false, convertedText); - } + emit recordTextUpdated(currentIndex, newData.toUtf8(), false); } else { // The data source is the hex widget buffer, thus binary data + QByteArray oldData = currentIndex.data(Qt::EditRole).toByteArray(); QByteArray newData = hexEdit->data(); - if ((newData != oldData) || (newData.isNull() != oldData.isNull())) { - // The data is different, so commit it back to the database - emit recordTextUpdated(curRow, curCol, true, newData); - } + if (newData != oldData) + emit recordTextUpdated(currentIndex, newData, true); } } diff --git a/src/EditDialog.h b/src/EditDialog.h index f6390e73..38af2d52 100644 --- a/src/EditDialog.h +++ b/src/EditDialog.h @@ -2,6 +2,7 @@ #define EDITDIALOG_H #include +#include class QHexEdit; @@ -17,18 +18,14 @@ public: explicit EditDialog(QWidget* parent = 0); ~EditDialog(); - int getCurrentCol() { return curCol; } - int getCurrentRow() { return curRow; } + void setCurrentIndex(const QModelIndex& idx); public slots: - virtual void reset(); - virtual void loadDataFromCell(const QByteArray& data, int row, int col); virtual void setFocus(); virtual void reject(); virtual void allowEditing(bool on); protected: - virtual void closeEvent(QCloseEvent* ev); virtual void showEvent(QShowEvent* ev); private slots: @@ -44,15 +41,12 @@ private slots: virtual QString humanReadableSize(double byteCount); signals: - void goingAway(); - void recordTextUpdated(int row, int col, bool isBlob, const QByteArray& data); + void recordTextUpdated(const QPersistentModelIndex& idx, const QByteArray& data, bool isBlob); private: Ui::EditDialog* ui; QHexEdit* hexEdit; - QByteArray oldData; - int curCol; - int curRow; + QPersistentModelIndex currentIndex; int dataSource; int dataType; diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 15fe663b..9f955ec5 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -202,11 +202,11 @@ void MainWindow::init() connect(ui->dataTable->filterHeader(), SIGNAL(sectionClicked(int)), this, SLOT(browseTableHeaderClicked(int))); connect(ui->dataTable->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(setRecordsetLabel())); connect(ui->dataTable->horizontalHeader(), SIGNAL(sectionResized(int,int,int)), this, SLOT(updateBrowseDataColumnWidth(int,int,int))); - connect(editDock, SIGNAL(goingAway()), this, SLOT(editDockAway())); - connect(editDock, SIGNAL(recordTextUpdated(int, int, bool, QByteArray)), this, SLOT(updateRecordText(int, int, bool, QByteArray))); + connect(editDock, SIGNAL(recordTextUpdated(QPersistentModelIndex, QByteArray, bool)), this, SLOT(updateRecordText(QPersistentModelIndex, QByteArray, bool))); connect(ui->dbTreeWidget->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(changeTreeSelection())); connect(ui->dataTable->horizontalHeader(), SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showDataColumnPopupMenu(QPoint))); connect(ui->dataTable->verticalHeader(), SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showRecordPopupMenu(QPoint))); + connect(ui->dockEdit, SIGNAL(visibilityChanged(bool)), this, SLOT(toggleEditDock(bool))); // plot widgets ui->treePlotColumns->setSelectionMode(QAbstractItemView::NoSelection); @@ -476,9 +476,6 @@ void MainWindow::populateTable(QString tablename) // Set the recordset label setRecordsetLabel(); - // Reset the edit cell dock - editDock->reset(); - // update plot updatePlot(m_browseTableModel); @@ -531,9 +528,6 @@ bool MainWindow::fileClose() connect(ui->dataTable->filterHeader(), SIGNAL(filterChanged(int,QString)), this, SLOT(updateFilter(int,QString))); connect(m_browseTableModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(dataTableSelectionChanged(QModelIndex))); - // Reset the edit cell dock - editDock->reset(); - // Remove all stored table information browse data tab browseTableSettings.clear(); defaultBrowseTableEncoding = QString(); @@ -805,23 +799,21 @@ void MainWindow::helpAbout() dialog.exec(); } -void MainWindow::updateRecordText(int row, int col, bool isBlob, const QByteArray& newtext) +void MainWindow::updateRecordText(const QPersistentModelIndex& idx, const QByteArray& text, bool isBlob) { - m_currentTabTableModel->setTypedData(m_currentTabTableModel->index(row, col), isBlob, newtext); + m_currentTabTableModel->setTypedData(idx, isBlob, text); } -void MainWindow::editDockAway() +void MainWindow::toggleEditDock(bool visible) { - // Get the sender - EditDialog* sendingEditDialog = qobject_cast(sender()); - - // Hide the edit dock - ui->dockEdit->setVisible(false); - - // Update main window - activateWindow(); - ui->dataTable->setFocus(); - ui->dataTable->setCurrentIndex(ui->dataTable->currentIndex().sibling(sendingEditDialog->getCurrentRow(), sendingEditDialog->getCurrentCol())); + if (!visible) { + // Update main window + activateWindow(); + ui->dataTable->setFocus(); + } else { + // fill edit dock with actual data + editDock->setCurrentIndex(ui->dataTable->currentIndex()); + } } void MainWindow::doubleClickTable(const QModelIndex& index) @@ -832,18 +824,14 @@ void MainWindow::doubleClickTable(const QModelIndex& index) } // * Don't allow editing of other objects than tables (on the browse table) * - bool allowEditing = (m_currentTabTableModel == m_browseTableModel) && + bool isEditingAllowed = (m_currentTabTableModel == m_browseTableModel) && (db.getObjectByName(ui->comboBrowseTable->currentText()).gettype() == "table"); // Enable or disable the Apply, Null, & Import buttons in the Edit Cell - // dock depending on the value of the "allowEditing" bool above - editDock->allowEditing(allowEditing); + // dock depending on the value of the "isEditingAllowed" bool above + editDock->allowEditing(isEditingAllowed); - // Load the current value into the edit dock - QByteArray cellData = index.data(Qt::EditRole).toByteArray(); - int cellRow = index.row(); - int cellColumn = index.column(); - editDock->loadDataFromCell(cellData, cellRow, cellColumn); + editDock->setCurrentIndex(index); // Show the edit dock ui->dockEdit->setVisible(true); @@ -855,21 +843,20 @@ void MainWindow::doubleClickTable(const QModelIndex& index) void MainWindow::dataTableSelectionChanged(const QModelIndex& index) { // Cancel on invalid index - if(!index.isValid()) + if(!index.isValid()) { + editDock->setCurrentIndex(QModelIndex()); return; + } - bool edit = (m_currentTabTableModel == m_browseTableModel) && - (db.getObjectByName(ui->comboBrowseTable->currentText()).gettype() == "table"); + bool editingAllowed = (m_currentTabTableModel == m_browseTableModel) && + (db.getObjectByName(ui->comboBrowseTable->currentText()).gettype() == "table"); // Don't allow editing of other objects than tables - editDock->allowEditing(edit); + editDock->allowEditing(editingAllowed); // If the Edit Cell dock is visible, load the new value into it if (editDock->isVisible()) { - QByteArray cellData = index.data(Qt::EditRole).toByteArray(); - int cellRow = index.row(); - int cellColumn = index.column(); - editDock->loadDataFromCell(cellData, cellRow, cellColumn); + editDock->setCurrentIndex(index); } } diff --git a/src/MainWindow.h b/src/MainWindow.h index 856af46c..5a40668a 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -179,8 +179,8 @@ private slots: void editTable(); void helpWhatsThis(); void helpAbout(); - void updateRecordText(int row, int col, bool type, const QByteArray& newtext); - void editDockAway(); + void updateRecordText(const QPersistentModelIndex& idx, const QByteArray& text, bool isBlob); + void toggleEditDock(bool visible); void dataTableSelectionChanged(const QModelIndex& index); void doubleClickTable(const QModelIndex& index); void executeQuery();