diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 8957c6e9..3846ec14 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -472,6 +472,9 @@ void MainWindow::populateTable() // Encoding m_browseTableModel->setEncoding(tableIt.value().encoding); + + // Plot + updatePlot(m_browseTableModel, true, false); } else { // There aren't any information stored for this table yet, so use some default values @@ -489,6 +492,9 @@ void MainWindow::populateTable() // Encoding m_browseTableModel->setEncoding(defaultBrowseTableEncoding); + // Plot + updatePlot(m_browseTableModel); + // The filters can be left empty as they are } @@ -501,9 +507,6 @@ void MainWindow::populateTable() // Set the recordset label setRecordsetLabel(); - // update plot - updatePlot(m_browseTableModel); - QApplication::restoreOverrideCursor(); } @@ -1815,7 +1818,7 @@ QVariant::Type guessdatatype(SqliteTableModel* model, int column) } } -void MainWindow::updatePlot(SqliteTableModel *model, bool update) +void MainWindow::updatePlot(SqliteTableModel *model, bool update, bool keepOrResetSelection) { // add columns to x/y selection tree widget if(update) @@ -1829,15 +1832,29 @@ void MainWindow::updatePlot(SqliteTableModel *model, bool update) // save current selected columns, so we can restore them after the update QString sItemX; // selected X column QMap mapItemsY; // selected Y columns with color - for(int i = 0; i < ui->treePlotColumns->topLevelItemCount(); ++i) - { - QTreeWidgetItem* item = ui->treePlotColumns->topLevelItem(i); - if(item->checkState(PlotColumnX) == Qt::Checked) - sItemX = item->text(PlotColumnField); - if(item->checkState(PlotColumnY) == Qt::Checked) + if(keepOrResetSelection) + { + // Store the currently selected plot columns to restore them later + for(int i = 0; i < ui->treePlotColumns->topLevelItemCount(); ++i) { - mapItemsY[item->text(PlotColumnField)] = item->backgroundColor(PlotColumnY); + QTreeWidgetItem* item = ui->treePlotColumns->topLevelItem(i); + if(item->checkState(PlotColumnX) == Qt::Checked) + sItemX = item->text(PlotColumnField); + + if(item->checkState(PlotColumnY) == Qt::Checked) + mapItemsY[item->text(PlotColumnField)] = item->backgroundColor(PlotColumnY); + } + } else { + // Get the plot columns to select from the stored browse table information + sItemX = browseTableSettings[ui->comboBrowseTable->currentText()].plotXAxis; + + QMap axesY = browseTableSettings[ui->comboBrowseTable->currentText()].plotYAxes; + QMap::ConstIterator it = axesY.constBegin(); + while(it != axesY.constEnd()) + { + mapItemsY.insert(it.key(), it.value().colour); + ++it; } } @@ -1989,6 +2006,7 @@ void MainWindow::on_treePlotColumns_itemChanged(QTreeWidgetItem *changeitem, int this,SLOT(on_treePlotColumns_itemChanged(QTreeWidgetItem*,int))); // make sure only 1 X axis is selected + QString current_table = ui->comboBrowseTable->currentText(); if(column == PlotColumnX) { for(int i = 0; i < ui->treePlotColumns->topLevelItemCount(); ++i) @@ -1999,9 +2017,13 @@ void MainWindow::on_treePlotColumns_itemChanged(QTreeWidgetItem *changeitem, int item->setCheckState(column, Qt::Unchecked); } } - } - else if(column == PlotColumnY) - { + + // Save settings for this table + if(changeitem->checkState(column) == Qt::Checked) + browseTableSettings[current_table].plotXAxis = changeitem->text(PlotColumnField); + else + browseTableSettings[current_table].plotXAxis = QString(); + } else if(column == PlotColumnY) { if(changeitem->checkState(column) == Qt::Checked) { // get a default color @@ -2010,10 +2032,17 @@ void MainWindow::on_treePlotColumns_itemChanged(QTreeWidgetItem *changeitem, int if(color.isValid()) { changeitem->setBackgroundColor(column, color); - } - else - { + + // Save settings for this table + PlotSettings& plot_settings = browseTableSettings[current_table].plotYAxes[changeitem->text(PlotColumnField)]; + plot_settings.colour = color; + plot_settings.lineStyle = ui->comboLineType->currentIndex(); + plot_settings.pointShape = (ui->comboPointShape->currentIndex() > 0 ? (ui->comboPointShape->currentIndex()+1) : ui->comboPointShape->currentIndex()); + } else { changeitem->setCheckState(column, Qt::Unchecked); + + // Save settings for this table + browseTableSettings[current_table].plotYAxes.remove(changeitem->text(PlotColumnField)); } } } @@ -2037,14 +2066,22 @@ void MainWindow::on_treePlotColumns_itemDoubleClicked(QTreeWidgetItem *item, int QColor curbkcolor = item->backgroundColor(column); QColor precolor = !curbkcolor.isValid() ? (Qt::GlobalColor)(qrand() % 13 + 5) : curbkcolor; QColor color = colordialog.getColor(precolor, this, tr("Choose a axis color")); + QString current_table = ui->comboBrowseTable->currentText(); if(color.isValid()) { item->setCheckState(column, Qt::Checked); item->setBackgroundColor(column, color); - } - else - { + + // Save settings for this table + PlotSettings& plot_settings = browseTableSettings[current_table].plotYAxes[item->text(PlotColumnField)]; + plot_settings.colour = color; + plot_settings.lineStyle = ui->comboLineType->currentIndex(); + plot_settings.pointShape = (ui->comboPointShape->currentIndex() > 0 ? (ui->comboPointShape->currentIndex()+1) : ui->comboPointShape->currentIndex()); + } else { item->setCheckState(column, Qt::Unchecked); + + // Save settings for this table + browseTableSettings[current_table].plotYAxes.remove(item->text(PlotColumnField)); } } @@ -2485,6 +2522,15 @@ void MainWindow::on_comboLineType_currentIndexChanged(int index) graph->setLineStyle(lineStyle); } ui->plotWidget->replot(); + + // Save settings for this table + QMap& graphs = browseTableSettings[ui->comboBrowseTable->currentText()].plotYAxes; + QMap::Iterator it = graphs.begin(); + while(it != graphs.end()) + { + it.value().lineStyle = lineStyle; + ++it; + } } void MainWindow::on_comboPointShape_currentIndexChanged(int index) @@ -2501,6 +2547,15 @@ void MainWindow::on_comboPointShape_currentIndexChanged(int index) graph->setScatterStyle(QCPScatterStyle(shape, 5)); } ui->plotWidget->replot(); + + // Save settings for this table + QMap& graphs = browseTableSettings[ui->comboBrowseTable->currentText()].plotYAxes; + QMap::Iterator it = graphs.begin(); + while(it != graphs.end()) + { + it.value().pointShape = shape; + ++it; + } } void MainWindow::jumpToRow(const QString& table, QString column, const QByteArray& value) diff --git a/src/MainWindow.h b/src/MainWindow.h index a463c58d..66b7fe61 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -33,6 +33,30 @@ public: DBBrowserDB& getDb() { return db; } + struct PlotSettings + { + int lineStyle; + int pointShape; + QColor colour; + + friend QDataStream& operator<<(QDataStream& stream, const MainWindow::PlotSettings& object) + { + stream << object.lineStyle; + stream << object.pointShape; + stream << object.colour; + + return stream; + } + friend QDataStream& operator>>(QDataStream& stream, MainWindow::PlotSettings& object) + { + stream >> object.lineStyle; + stream >> object.pointShape; + stream >> object.colour; + + return stream; + } + }; + struct BrowseDataTableSettings { int sortOrderIndex; @@ -42,6 +66,8 @@ public: QMap displayFormats; bool showRowid; QString encoding; + QString plotXAxis; + QMap plotYAxes; friend QDataStream& operator<<(QDataStream& stream, const MainWindow::BrowseDataTableSettings& object) { @@ -52,6 +78,8 @@ public: stream << object.displayFormats; stream << object.showRowid; stream << object.encoding; + stream << object.plotXAxis; + stream << object.plotYAxes; return stream; } @@ -67,6 +95,15 @@ public: stream >> object.showRowid; stream >> object.encoding; + // Versions pre 3.10.0 didn't store the following information in their project files. + // To be absolutely sure that nothing strange happens when we read past the stream for + // those cases, check for the end of the stream here. + if(stream.atEnd()) + return stream; + + stream >> object.plotXAxis; + stream >> object.plotYAxes; + return stream; } }; @@ -218,7 +255,7 @@ private slots: void loadExtension(); void reloadSettings(); void httpresponse(QNetworkReply* reply); - void updatePlot(SqliteTableModel* model, bool update = true); + void updatePlot(SqliteTableModel* model, bool update = true, bool keepOrResetSelection = true); void on_treePlotColumns_itemChanged(QTreeWidgetItem *item, int column); void on_treePlotColumns_itemDoubleClicked(QTreeWidgetItem *item, int column); void on_butSavePlot_clicked();