开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
forked from 陈炳煌/sqlitebrowser
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (16)
标签 (31)
master
coverity_scan
v3.11.x
issue1831_v1
qdarkstyle
no_savepoints_ro_db
v3.10.x
v3.9.x
v3.6.x
v3.5.x
v3.4.x
v3.3.x
utf8grammar
sqlb-3.2.x
sqlb-3.1.x
sqlb-3.x
continuous
v3.11.2
v3.11.1v2
v3.11.1
v3.11.0
v3.11.0-beta3
v3.11.0-beta2
v3.11.0-beta1
v3.11.0-alpha1
v3.10.1
v3.10.0
v3.10.0-beta2
v3.10.0-beta1
v3.9.1
v3.9.0
v3.9.0-beta1
v3.8.0
v3.7.0
v3.6.0
v3.5.1
master
分支 (16)
标签 (31)
master
coverity_scan
v3.11.x
issue1831_v1
qdarkstyle
no_savepoints_ro_db
v3.10.x
v3.9.x
v3.6.x
v3.5.x
v3.4.x
v3.3.x
utf8grammar
sqlb-3.2.x
sqlb-3.1.x
sqlb-3.x
continuous
v3.11.2
v3.11.1v2
v3.11.1
v3.11.0
v3.11.0-beta3
v3.11.0-beta2
v3.11.0-beta1
v3.11.0-alpha1
v3.10.1
v3.10.0
v3.10.0-beta2
v3.10.0-beta1
v3.9.1
v3.9.0
v3.9.0-beta1
v3.8.0
v3.7.0
v3.6.0
v3.5.1
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (16)
标签 (31)
master
coverity_scan
v3.11.x
issue1831_v1
qdarkstyle
no_savepoints_ro_db
v3.10.x
v3.9.x
v3.6.x
v3.5.x
v3.4.x
v3.3.x
utf8grammar
sqlb-3.2.x
sqlb-3.1.x
sqlb-3.x
continuous
v3.11.2
v3.11.1v2
v3.11.1
v3.11.0
v3.11.0-beta3
v3.11.0-beta2
v3.11.0-beta1
v3.11.0-alpha1
v3.10.1
v3.10.0
v3.10.0-beta2
v3.10.0-beta1
v3.9.1
v3.9.0
v3.9.0-beta1
v3.8.0
v3.7.0
v3.6.0
v3.5.1
sqlitebrowser
/
src
/
PlotDock.cpp
sqlitebrowser
/
src
/
PlotDock.cpp
PlotDock.cpp 35.72 KB
一键复制 编辑 原始数据 按行查看 历史
Scott Furry 提交于 2019年05月08日 04:09 +08:00 . Shadowed Variable Warnings (#1864)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
#include "PlotDock.h"
#include "ui_PlotDock.h"
#include "Settings.h"
#include "sqlitetablemodel.h"
#include "FileDialog.h"
#include "MainWindow.h" // Just for BrowseDataTableSettings, not for the actual main window class
#include <QPrinter>
#include <QPrintPreviewDialog>
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
#include <QRandomGenerator>
#endif
// Enable this line to show the most basic performance stats after pressing the fetch-all-data button. Please keep in mind that while these
// numbers might help to estimate the performance of the data loading procedure, this is not a proper benchmark.
//#define LOAD_DATA_BENCHMARK
#ifdef LOAD_DATA_BENCHMARK
#include <QElapsedTimer>
#endif
static int random_number(int from, int to)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
return QRandomGenerator::global()->bounded(from, to);
#else
return qrand() % to + from;
#endif
}
PlotDock::PlotDock(QWidget* parent)
: QDialog(parent),
ui(new Ui::PlotDock),
m_currentPlotModel(nullptr),
m_currentTableSettings(nullptr),
m_showLegend(false),
m_stackedBars(false)
{
ui->setupUi(this);
// Init widgets
ui->treePlotColumns->setSelectionMode(QAbstractItemView::NoSelection);
// Restore state
ui->splitterForPlot->restoreState(Settings::getValue("PlotDock", "splitterSize").toByteArray());
ui->comboLineType->setCurrentIndex(Settings::getValue("PlotDock", "lineType").toInt());
ui->comboPointShape->setCurrentIndex(Settings::getValue("PlotDock", "pointShape").toInt());
// Connect signals
connect(ui->treePlotColumns, &QTreeWidget::itemChanged, this, &PlotDock::on_treePlotColumns_itemChanged);
connect(ui->plotWidget, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged()));
// connect slots that takes care that when an axis is selected, only that direction can be dragged and zoomed:
connect(ui->plotWidget, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress()));
connect(ui->plotWidget, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel()));
// Enable: click on items to select them, Ctrl+Click for multi-selection, mouse-wheel for zooming and mouse drag for
// changing the visible range.
// Select one axis for zoom and drag applying only to that orientation.
ui->plotWidget->setInteractions(QCP::iSelectPlottables | QCP::iMultiSelect | QCP::iRangeZoom | QCP::iRangeDrag | QCP::iSelectAxes);
ui->plotWidget->setSelectionRectMode(QCP::srmNone);
QShortcut* shortcutCopy = new QShortcut(QKeySequence::Copy, ui->plotWidget, nullptr, nullptr, Qt::WidgetShortcut);
connect(shortcutCopy, SIGNAL(activated()), this, SLOT(copy()));
QShortcut* shortcutPrint = new QShortcut(QKeySequence::Print, ui->plotWidget, nullptr, nullptr, Qt::WidgetShortcut);
connect(shortcutPrint, &QShortcut::activated, this, &PlotDock::openPrintDialog);
ui->plotWidget->setContextMenuPolicy(Qt::CustomContextMenu);
// Set up context menu
m_contextMenu = new QMenu(this);
QAction* copyAction = new QAction(QIcon(":/icons/copy"), tr("Copy"), m_contextMenu);
copyAction->setShortcut(shortcutCopy->key());
m_contextMenu->addAction(copyAction);
connect(copyAction, &QAction::triggered, [&]() {
copy();
});
QAction* printAction = new QAction(QIcon(":/icons/print"), tr("Print..."), m_contextMenu);
printAction->setShortcut(shortcutPrint->key());
m_contextMenu->addAction(printAction);
connect(printAction, &QAction::triggered, [&]() {
openPrintDialog();
});
QAction* showLegendAction = new QAction(tr("Show legend"), m_contextMenu);
showLegendAction->setCheckable(true);
m_contextMenu->addAction(showLegendAction);
connect(showLegendAction, SIGNAL(toggled(bool)), this, SLOT(toggleLegendVisible(bool)));
QAction* stackedBarsAction = new QAction(tr("Stacked bars"), m_contextMenu);
stackedBarsAction->setCheckable(true);
m_contextMenu->addAction(stackedBarsAction);
connect(stackedBarsAction, SIGNAL(toggled(bool)), this, SLOT(toggleStackedBars(bool)));
connect(ui->plotWidget, &QTableView::customContextMenuRequested,
[=](const QPoint& pos) {
// Show menu
m_contextMenu->popup(ui->plotWidget->mapToGlobal(pos));
});
}
PlotDock::~PlotDock()
{
// Save state
Settings::setValue("PlotDock", "splitterSize", ui->splitterForPlot->saveState());
Settings::setValue("PlotDock", "lineType", ui->comboLineType->currentIndex());
Settings::setValue("PlotDock", "pointShape", ui->comboPointShape->currentIndex());
// Finally, delete all widgets
delete ui;
}
void PlotDock::updatePlot(SqliteTableModel* model, BrowseDataTableSettings* settings, bool update, bool keepOrResetSelection)
{
// Each column has an id that we use internally, starting from 0. However, at the beginning of the columns list we want to add
// the virtual 'Row #' column which needs a separate unique id for internal use. This id is defined here as -1.
const int RowNumId = -1;
// add columns to x/y selection tree widget
if(update)
{
// Store pointer to the current browse table settings in the main window
m_currentTableSettings = settings;
// disable tree plot columns item changed updates
ui->treePlotColumns->blockSignals(true);
m_currentPlotModel = model;
// save current selected columns, so we can restore them after the update
QString sItemX; // selected X column
QMap<QString, PlotSettings> mapItemsY; // selected Y columns with color
if(keepOrResetSelection)
{
// Store the currently selected plot columns to restore them later
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)
mapItemsY[item->text(PlotColumnField)] = PlotSettings(0, 0, item->backgroundColor(PlotColumnY), item->checkState(PlotColumnY) == Qt::Checked);
}
} else {
// Get the plot columns to select from the stored browse table information
sItemX = m_currentTableSettings->plotXAxis;
mapItemsY = m_currentTableSettings->plotYAxes;
}
ui->treePlotColumns->clear();
if(model)
{
// Add each column with a supported data type to the column selection view
for(int i=0;i<model->columnCount();++i)
{
QVariant::Type columntype = guessDataType(model, i);
if(columntype != QVariant::Invalid)
{
QTreeWidgetItem* columnitem = new QTreeWidgetItem(ui->treePlotColumns);
switch (columntype) {
case QVariant::DateTime:
columnitem->setText(PlotColumnType, tr("Date/Time"));
break;
case QVariant::Date:
columnitem->setText(PlotColumnType, tr("Date"));
break;
case QVariant::Time:
columnitem->setText(PlotColumnType, tr("Time"));
break;
case QVariant::Double:
columnitem->setText(PlotColumnType, tr("Numeric"));
break;
case QVariant::String:
columnitem->setText(PlotColumnType, tr("Label"));
break;
default:
// This is not actually expected
columnitem->setText(PlotColumnType, tr("Invalid"));
}
// Store the model column index in the PlotColumnField and the type
// in the PlotColumnType, both using the User Role.
columnitem->setData(PlotColumnField, Qt::UserRole, i);
columnitem->setData(PlotColumnType, Qt::UserRole, static_cast<int>(columntype));
columnitem->setText(PlotColumnField, model->headerData(i, Qt::Horizontal).toString());
// restore previous check state
if(mapItemsY.contains(columnitem->text(PlotColumnField)))
{
columnitem->setCheckState(PlotColumnY, mapItemsY[columnitem->text(PlotColumnField)].active ? Qt::Checked : Qt::Unchecked);
columnitem->setBackgroundColor(PlotColumnY, mapItemsY[columnitem->text(PlotColumnField)].colour);
} else {
if (columntype == QVariant::Double)
columnitem->setCheckState(PlotColumnY, Qt::Unchecked);
}
if(sItemX == columnitem->text(PlotColumnField))
columnitem->setCheckState(PlotColumnX, Qt::Checked);
else
columnitem->setCheckState(PlotColumnX, Qt::Unchecked);
}
}
ui->treePlotColumns->resizeColumnToContents(PlotColumnField);
// Add a row number column at the beginning of the column list, but only when there were (other) columns added
if(ui->treePlotColumns->topLevelItemCount())
{
QTreeWidgetItem* columnitem = new QTreeWidgetItem(ui->treePlotColumns);
// Just set RowNumId in the user role information field here to somehow indicate what column this is
columnitem->setData(PlotColumnField, Qt::UserRole, RowNumId);
columnitem->setText(PlotColumnField, tr("Row #"));
columnitem->setData(PlotColumnType, Qt::UserRole, static_cast<int>(QVariant::Double));
columnitem->setText(PlotColumnType, tr("Numeric"));
// restore previous check state
if(mapItemsY.contains(columnitem->text(PlotColumnField)))
{
columnitem->setCheckState(PlotColumnY, mapItemsY[columnitem->text(PlotColumnField)].active ? Qt::Checked : Qt::Unchecked);
columnitem->setBackgroundColor(PlotColumnY, mapItemsY[columnitem->text(PlotColumnField)].colour);
} else {
columnitem->setCheckState(PlotColumnY, Qt::Unchecked);
}
if(sItemX == columnitem->text(PlotColumnField))
columnitem->setCheckState(PlotColumnX, Qt::Checked);
else
columnitem->setCheckState(PlotColumnX, Qt::Unchecked);
ui->treePlotColumns->takeTopLevelItem(ui->treePlotColumns->indexOfTopLevelItem(columnitem));
ui->treePlotColumns->insertTopLevelItem(0, columnitem);
}
}
ui->plotWidget->yAxis->setLabel("Y");
ui->plotWidget->xAxis->setLabel("X");
ui->treePlotColumns->blockSignals(false);
}
// search for the x axis select
QTreeWidgetItem* xitem = nullptr;
for(int i = 0; i < ui->treePlotColumns->topLevelItemCount(); ++i)
{
xitem = ui->treePlotColumns->topLevelItem(i);
if(xitem->checkState(PlotColumnX) == Qt::Checked)
break;
xitem = nullptr;
}
QStringList yAxisLabels;
// Clear graphs and axis labels
ui->plotWidget->clearPlottables();
ui->plotWidget->xAxis->setLabel(QString());
ui->plotWidget->yAxis->setLabel(QString());
if(xitem)
{
// regain the model column index and the datatype
// right now datatype is only important for X axis (Y is always numeric)
int x = xitem->data(PlotColumnField, Qt::UserRole).toInt();
int xtype = xitem->data(PlotColumnType, Qt::UserRole).toInt();
ui->plotWidget->xAxis->setTickLabelRotation(0);
// check if we have a x axis with datetime data
switch (xtype) {
case QVariant::Date: {
QSharedPointer<QCPAxisTickerDateTime> ticker(new QCPAxisTickerDateTime);
ticker->setDateTimeFormat("yyyy-MM-dd");
ui->plotWidget->xAxis->setTicker(ticker);
break;
}
case QVariant::DateTime: {
QSharedPointer<QCPAxisTickerDateTime> ticker(new QCPAxisTickerDateTime);
ticker->setDateTimeFormat("yyyy-MM-dd\nhh:mm:ss");
ui->plotWidget->xAxis->setTicker(ticker);
break;
}
case QVariant::Time: {
QSharedPointer<QCPAxisTickerDateTime> ticker(new QCPAxisTickerDateTime);
ticker->setDateTimeFormat("hh:mm:ss");
ticker->setDateTimeSpec(Qt::UTC);
ui->plotWidget->xAxis->setTicker(ticker);
break;
}
case QVariant::String: {
// Ticker is set when we have got the labels
ui->plotWidget->xAxis->setTickLabelRotation(60);
break;
}
default: {
QSharedPointer<QCPAxisTickerFixed> ticker(new QCPAxisTickerFixed);
ticker->setTickStepStrategy(QCPAxisTicker::tssReadability);
ticker->setScaleStrategy(QCPAxisTickerFixed::ssMultiples);
ui->plotWidget->xAxis->setTicker(ticker);
}
}
// add graph for each selected y axis
for(int i = 0; i < ui->treePlotColumns->topLevelItemCount(); ++i)
{
QTreeWidgetItem* item = ui->treePlotColumns->topLevelItem(i);
if(item->checkState((PlotColumnY)) == Qt::Checked)
{
// regain the model column index
int column = item->data(PlotColumnField, Qt::UserRole).toInt();
bool isSorted = true;
// prepare the data vectors for qcustomplot
// possible improvement might be a QVector subclass that directly
// access the model data, to save memory, we are copying here
auto nrows = model->rowCount();
QVector<double> xdata(nrows), ydata(nrows), tdata(nrows);
QVector<QString> labels;
for(int j = 0; j < nrows; ++j)
{
tdata[j] = j;
// convert x type axis if it's datetime
switch (xtype) {
case QVariant::DateTime:
case QVariant::Date: {
QString s = model->data(model->index(j, x)).toString();
QDateTime d = QDateTime::fromString(s, Qt::ISODate);
xdata[j] = static_cast<double>(d.toMSecsSinceEpoch()) / 1000.0;
break;
}
case QVariant::Time: {
QString s = model->data(model->index(j, x)).toString();
QTime t = QTime::fromString(s);
xdata[j] = t.msecsSinceStartOfDay() / 1000.0;
break;
}
case QVariant::String: {
xdata[j] = j+1;
labels << model->data(model->index(j, x)).toString();
break;
}
default: {
// Get the x value for this point. If the selected column is -1, i.e. the row number, just use the current row number from the loop
// instead of retrieving some value from the model.
if(x == RowNumId)
xdata[j] = j+1;
else
xdata[j] = model->data(model->index(j, x)).toDouble();
}
}
if (j != 0)
isSorted &= (xdata[j-1] <= xdata[j]);
// Get the y value for this point. If the selected column is -1, i.e. the row number, just use the current row number from the loop
// instead of retrieving some value from the model.
QVariant pointdata;
if(column == RowNumId)
pointdata = j+1;
else
pointdata = model->data(model->index(j, column), Qt::EditRole);
if(pointdata.isNull())
ydata[j] = qQNaN();
else
ydata[j] = pointdata.toDouble();
}
// Line type and point shape are not supported by the String X type (Bars)
ui->comboLineType->setEnabled(xtype != QVariant::String);
ui->comboPointShape->setEnabled(xtype != QVariant::String);
// WARN: ssDot is removed
int shapeIdx = ui->comboPointShape->currentIndex();
if (shapeIdx > 0) shapeIdx += 1;
QCPScatterStyle scatterStyle = QCPScatterStyle(static_cast<QCPScatterStyle::ScatterShape>(shapeIdx), 5);
QCPAbstractPlottable* plottable;
// When the X type is String, we draw a bar chart.
// When it is already sorted by x, we draw a graph.
// When it is not sorted by x, we draw a curve, so the order selected by the user in the table or in the query is
// respected. In this case the line will have loops and only None and Line is supported as line style.
// TODO: how to make the user aware of this without disturbing.
if (xtype == QVariant::String) {
QCPBars* bars = new QCPBars(ui->plotWidget->xAxis, ui->plotWidget->yAxis);
plottable = bars;
bars->setData(xdata, ydata);
// Set ticker once
if (ui->plotWidget->plottableCount() == 1) {
QSharedPointer<QCPAxisTickerText> ticker(new QCPAxisTickerText);
ticker->addTicks(xdata, labels);
ui->plotWidget->xAxis->setTicker(ticker);
}
QColor color = item->backgroundColor(PlotColumnY);
bars->setBrush(color);
plottable->setPen(QPen(color.darker(150)));
} else {
if (isSorted) {
QCPGraph* graph = ui->plotWidget->addGraph();
plottable = graph;
graph->setData(xdata, ydata, /*alreadySorted*/ true);
// set some graph styles not supported by the abstract plottable
graph->setLineStyle(static_cast<QCPGraph::LineStyle>(ui->comboLineType->currentIndex()));
graph->setScatterStyle(scatterStyle);
} else {
QCPCurve* curve = new QCPCurve(ui->plotWidget->xAxis, ui->plotWidget->yAxis);
plottable = curve;
curve->setData(tdata, xdata, ydata, /*alreadySorted*/ true);
// set some curve styles not supported by the abstract plottable
if (ui->comboLineType->currentIndex() == QCPCurve::lsNone)
curve->setLineStyle(QCPCurve::lsNone);
else
curve->setLineStyle(QCPCurve::lsLine);
curve->setScatterStyle(scatterStyle);
}
plottable->setPen(QPen(item->backgroundColor(PlotColumnY)));
}
plottable->setSelectable(QCP::stDataRange);
plottable->setName(item->text(PlotColumnField));
// gather Y label column names
if(column == RowNumId)
yAxisLabels << tr("Row #");
else
yAxisLabels << model->headerData(column, Qt::Horizontal).toString();
}
}
ui->plotWidget->rescaleAxes(true);
ui->plotWidget->legend->setVisible(m_showLegend);
// Legend with slightly transparent background brush:
ui->plotWidget->legend->setBrush(QColor(255, 255, 255, 150));
// set axis labels
if(x == RowNumId)
ui->plotWidget->xAxis->setLabel(tr("Row #"));
else
ui->plotWidget->xAxis->setLabel(model->headerData(x, Qt::Horizontal).toString());
ui->plotWidget->yAxis->setLabel(yAxisLabels.join("|"));
}
adjustBars();
ui->plotWidget->replot();
// Warn user if not all data has been fetched and hint about the button for loading all the data
if (model && (model->rowCountAvailable() != SqliteTableModel::RowCount::Complete || !model->isCacheComplete())) {
ui->buttonLoadAllData->setEnabled(true);
ui->buttonLoadAllData->setStyleSheet("QToolButton {color: white; background-color: rgb(255, 102, 102)}");
ui->buttonLoadAllData->setToolTip(tr("Load all data and redraw plot.\n"
"Warning: not all data has been fetched from the table yet due to the partial fetch mechanism."));
} else {
ui->buttonLoadAllData->setEnabled(false);
ui->buttonLoadAllData->setStyleSheet("");
ui->buttonLoadAllData->setToolTip(tr("Load all data and redraw plot"));
}
}
void PlotDock::resetPlot()
{
updatePlot(nullptr);
}
void PlotDock::on_treePlotColumns_itemChanged(QTreeWidgetItem* changeitem, int column)
{
// disable change updates, or we get unwanted redrawing and weird behavior
ui->treePlotColumns->blockSignals(true);
// make sure only 1 X axis is selected
if(column == PlotColumnX)
{
for(int i = 0; i < ui->treePlotColumns->topLevelItemCount(); ++i)
{
QTreeWidgetItem* item = ui->treePlotColumns->topLevelItem(i);
if(item->checkState(column) == Qt::Checked && item != changeitem)
{
item->setCheckState(column, Qt::Unchecked);
}
}
// Save settings for this table
if(m_currentTableSettings)
{
if(changeitem->checkState(column) == Qt::Checked)
m_currentTableSettings->plotXAxis = changeitem->text(PlotColumnField);
else
m_currentTableSettings->plotXAxis = QString();
}
} else if(column == PlotColumnY) {
// Save check state of this column
if(m_currentTableSettings)
{
PlotSettings& plot_settings = m_currentTableSettings->plotYAxes[changeitem->text(PlotColumnField)];
plot_settings.active = (changeitem->checkState(column) == Qt::Checked);
}
if(changeitem->checkState(column) == Qt::Checked)
{
// Generate a default colour if none isn't set yet
QColor colour = changeitem->backgroundColor(column);
if(!colour.isValid())
colour = m_graphPalette.nextSerialColor(true);
// Set colour
changeitem->setBackgroundColor(column, colour);
// Save settings for this table
if(m_currentTableSettings)
{
PlotSettings& plot_settings = m_currentTableSettings->plotYAxes[changeitem->text(PlotColumnField)];
plot_settings.colour = colour;
plot_settings.lineStyle = ui->comboLineType->currentIndex();
plot_settings.pointShape = (ui->comboPointShape->currentIndex() > 0 ? (ui->comboPointShape->currentIndex()+1) : ui->comboPointShape->currentIndex());
}
}
}
ui->treePlotColumns->blockSignals(false);
updatePlot(m_currentPlotModel, m_currentTableSettings, false);
}
void PlotDock::on_treePlotColumns_itemDoubleClicked(QTreeWidgetItem* item, int column)
{
// disable change updates, or we get unwanted redrawing and weird behavior
ui->treePlotColumns->blockSignals(true);
int type = item->data(PlotColumnType, Qt::UserRole).toInt();
if(column == PlotColumnY && type == QVariant::Double)
{
// On double click open the colordialog
QColorDialog colordialog(this);
QColor curbkcolor = item->backgroundColor(column);
QColor precolor = !curbkcolor.isValid() ? static_cast<Qt::GlobalColor>(random_number(5, 13)) : curbkcolor;
QColor color = colordialog.getColor(precolor, this, tr("Choose an axis color"));
if(color.isValid())
{
item->setCheckState(column, Qt::Checked);
item->setBackgroundColor(column, color);
// Save settings for this table
if(m_currentTableSettings)
{
PlotSettings& plot_settings = m_currentTableSettings->plotYAxes[item->text(PlotColumnField)];
plot_settings.active = (item->checkState(column) == Qt::Checked);
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
if(m_currentTableSettings)
m_currentTableSettings->plotYAxes.remove(item->text(PlotColumnField));
}
}
ui->treePlotColumns->blockSignals(false);
updatePlot(m_currentPlotModel, m_currentTableSettings, false);
}
void PlotDock::on_butSavePlot_clicked()
{
QString fileName = FileDialog::getSaveFileName(
CreateDataFile,
this,
tr("Choose a filename to save under"),
tr("PNG(*.png);;JPG(*.jpg);;PDF(*.pdf);;BMP(*.bmp);;All Files(*)"));
if(!fileName.isEmpty())
{
if(fileName.endsWith(".png", Qt::CaseInsensitive))
{
ui->plotWidget->savePng(fileName);
}
else if(fileName.endsWith(".jpg", Qt::CaseInsensitive))
{
ui->plotWidget->saveJpg(fileName);
}
else if(fileName.endsWith(".pdf", Qt::CaseInsensitive))
{
ui->plotWidget->savePdf(fileName);
}
else if(fileName.endsWith(".bmp", Qt::CaseInsensitive))
{
ui->plotWidget->saveBmp(fileName);
}
else
{
fileName += ".png";
ui->plotWidget->savePng(fileName);
}
}
}
void PlotDock::on_comboLineType_currentIndexChanged(int index)
{
Q_ASSERT(index >= QCPGraph::lsNone &&
index <= QCPGraph::lsImpulse);
bool hasCurves = (ui->plotWidget->plottableCount() > ui->plotWidget->graphCount());
QCPGraph::LineStyle lineStyle = static_cast<QCPGraph::LineStyle>(index);
if (lineStyle > QCPGraph::lsLine && hasCurves) {
QMessageBox::warning(this, qApp->applicationName(),
tr("There are curves in this plot and the selected line style can only be applied to graphs sorted by X. "
"Either sort the table or query by X to remove curves or select one of the styles supported by curves: "
"None or Line."));
return;
}
for (int i = 0, ie = ui->plotWidget->graphCount(); i < ie; ++i)
{
QCPGraph * graph = ui->plotWidget->graph(i);
if (graph)
graph->setLineStyle(lineStyle);
}
// We have changed the style only for graphs, but not for curves.
// If there are any in the plot, we have to update it completely in order to apply the new style
if (hasCurves)
updatePlot(m_currentPlotModel, m_currentTableSettings, false);
else
ui->plotWidget->replot();
// Save settings for this table
if(m_currentTableSettings)
{
QMap<QString, PlotSettings>& graphs = m_currentTableSettings->plotYAxes;
auto it = graphs.begin();
while(it != graphs.end())
{
it.value().lineStyle = lineStyle;
++it;
}
}
}
void PlotDock::on_comboPointShape_currentIndexChanged(int index)
{
// WARN: because ssDot point shape is removed
if (index > 0) index += 1;
Q_ASSERT(index >= QCPScatterStyle::ssNone &&
index < QCPScatterStyle::ssPixmap);
bool hasCurves = (ui->plotWidget->plottableCount() > ui->plotWidget->graphCount());
QCPScatterStyle::ScatterShape shape = static_cast<QCPScatterStyle::ScatterShape>(index);
for (int i = 0, ie = ui->plotWidget->graphCount(); i < ie; ++i)
{
QCPGraph * graph = ui->plotWidget->graph(i);
if (graph)
graph->setScatterStyle(QCPScatterStyle(shape, 5));
}
// We have changed the style only for graphs, but not for curves.
// If there are any in the plot, we have to update it completely in order to apply the new style
if (hasCurves)
updatePlot(m_currentPlotModel, m_currentTableSettings, false);
else
ui->plotWidget->replot();
// Save settings for this table
if(m_currentTableSettings)
{
QMap<QString, PlotSettings>& graphs = m_currentTableSettings->plotYAxes;
auto it = graphs.begin();
while(it != graphs.end())
{
it.value().pointShape = shape;
++it;
}
}
}
QVariant::Type PlotDock::guessDataType(SqliteTableModel* model, int column)
{
QVariant::Type type = QVariant::Invalid;
for(int i = 0; i < std::min(10, model->rowCount()) && type != QVariant::String; ++i)
{
QVariant varData = model->data(model->index(i, column), Qt::EditRole);
if(varData.isNull() || varData.convert(QVariant::Double))
{
type = QVariant::Double;
} else {
QString s = model->data(model->index(i, column)).toString();
QDateTime dt = QDateTime::fromString(s, Qt::ISODate);
QTime t = QTime::fromString(s);
if (dt.isValid())
// Since the way to discriminate dates with times and pure dates is that the time part is 0, we must take into account
// that some DateTimes could have "00:00:00" as time part and still the entire column has time information, so a single
// final Date should not set the type to Date if it has already been guessed as DateTime.
if (type != QVariant::DateTime && dt.time().msecsSinceStartOfDay() == 0)
type = QVariant::Date;
else
type = QVariant::DateTime;
else if (t.isValid())
type = QVariant::Time;
else
type = QVariant::String;
}
}
return type;
}
void PlotDock::fetchAllData()
{
if(m_currentPlotModel)
{
#ifdef LOAD_DATA_BENCHMARK
// If benchmark mode is enabled start measuring the performance now
QElapsedTimer timer;
timer.start();
#endif
// Make sure all data is loaded
m_currentPlotModel->completeCache();
#ifdef LOAD_DATA_BENCHMARK
QMessageBox::information(this, qApp->applicationName(),
tr("Loading all remaining data for this table took %1ms.")
.arg(timer.elapsed()));
#endif
// Update plot
updatePlot(m_currentPlotModel, m_currentTableSettings);
}
}
void PlotDock::selectionChanged()
{
for (QCPAbstractPlottable* plottable : ui->plotWidget->selectedPlottables()) {
for (QCPDataRange dataRange : plottable->selection().dataRanges()) {
int index = dataRange.begin();
if (dataRange.length() != 0) {
emit pointsSelected(index, dataRange.length());
break;
}
}
}
}
void PlotDock::mousePress()
{
// Allow user to reset the plot
ui->buttonLoadAllData->setEnabled(true);
// if an axis (or axis labels) is selected, only allow the direction of that axis to be dragged
// if no axis (or axis labels) is selected, both directions may be dragged
if (ui->plotWidget->xAxis->selectedParts().testFlag(QCPAxis::spAxis) ||
ui->plotWidget->xAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
ui->plotWidget->xAxis->selectedParts().testFlag(QCPAxis::spAxisLabel))
ui->plotWidget->axisRect()->setRangeDrag(ui->plotWidget->xAxis->orientation());
else if (ui->plotWidget->yAxis->selectedParts().testFlag(QCPAxis::spAxis) ||
ui->plotWidget->yAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
ui->plotWidget->yAxis->selectedParts().testFlag(QCPAxis::spAxisLabel))
ui->plotWidget->axisRect()->setRangeDrag(ui->plotWidget->yAxis->orientation());
else
ui->plotWidget->axisRect()->setRangeDrag(Qt::Horizontal | Qt::Vertical);
}
void PlotDock::mouseWheel()
{
// Allow user to reset the plot
ui->buttonLoadAllData->setEnabled(true);
// if an axis (or axis labels) is selected, only allow the direction of that axis to be zoomed
// if no axis (or axis labels) is selected, both directions may be zoomed
if (ui->plotWidget->xAxis->selectedParts().testFlag(QCPAxis::spAxis) ||
ui->plotWidget->xAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
ui->plotWidget->xAxis->selectedParts().testFlag(QCPAxis::spAxisLabel))
ui->plotWidget->axisRect()->setRangeZoom(ui->plotWidget->xAxis->orientation());
else if (ui->plotWidget->yAxis->selectedParts().testFlag(QCPAxis::spAxis) ||
ui->plotWidget->yAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
ui->plotWidget->yAxis->selectedParts().testFlag(QCPAxis::spAxisLabel))
ui->plotWidget->axisRect()->setRangeZoom(ui->plotWidget->yAxis->orientation());
else
ui->plotWidget->axisRect()->setRangeZoom(Qt::Horizontal | Qt::Vertical);
}
void PlotDock::copy()
{
QApplication::clipboard()->setPixmap(ui->plotWidget->toPixmap());
}
void PlotDock::toggleLegendVisible(bool visible)
{
m_showLegend = visible;
ui->plotWidget->legend->setVisible(m_showLegend);
ui->plotWidget->replot();
}
// Stack or group bars and set the appropiate bar width (since it is not automatically done by QCustomPlot).
void PlotDock::adjustBars()
{
const double padding = 0.15;
int plottableCount = ui->plotWidget->plottableCount();
if (plottableCount == 0)
return;
const double groupedWidth = 1.0 / plottableCount;
QCPBars* previousBar = nullptr;
QCPBarsGroup* barsGroup = m_stackedBars ? nullptr : new QCPBarsGroup(ui->plotWidget);
for (int i = 0, ie = plottableCount; i < ie; ++i)
{
QCPBars* bar = qobject_cast<QCPBars*>(ui->plotWidget->plottable(i));
if (bar) {
if (m_stackedBars) {
// Ungroup if grouped
bar->setBarsGroup(nullptr);
if (previousBar)
bar->moveAbove(previousBar);
// Set width to ocuppy the full coordinate space, less padding
bar->setWidth(1.0 - padding);
} else {
// Unstack if stacked
bar->moveAbove(nullptr);
bar->setBarsGroup(barsGroup);
// Set width to a plot coordinate width, less padding
bar->setWidth(groupedWidth - padding);
}
previousBar = bar;
}
}
}
void PlotDock::toggleStackedBars(bool stacked)
{
m_stackedBars = stacked;
adjustBars();
ui->plotWidget->replot();
}
void PlotDock::reject()
{
// We override this, to ensure the Escape key doesn't make this dialog
// dock go away
return;
}
void PlotDock::openPrintDialog()
{
QPrinter printer;
QPrintPreviewDialog previewDialog(&printer, this);
connect(&previewDialog, &QPrintPreviewDialog::paintRequested, this, &PlotDock::renderPlot);
previewDialog.exec();
}
void PlotDock::renderPlot(QPrinter* printer)
{
QCPPainter painter(printer);
QRectF pageRect = printer->pageRect(QPrinter::DevicePixel);
int plotWidth = ui->plotWidget->viewport().width();
int plotHeight = ui->plotWidget->viewport().height();
double scale = pageRect.width()/static_cast<double>(plotWidth);
painter.setMode(QCPPainter::pmVectorized);
painter.setMode(QCPPainter::pmNoCaching);
painter.scale(scale, scale);
ui->plotWidget->toPainter(&painter, plotWidth, plotHeight);
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

Official home of the DB Browser for SQLite (DB4S) project. Previously known as "SQLite Database Browser" and "Database Browser for SQLite". Website at:
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/my_open_source_software/sqlitebrowser.git
git@gitee.com:my_open_source_software/sqlitebrowser.git
my_open_source_software
sqlitebrowser
sqlitebrowser
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /