0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-25 00:54:39 +03:00
LimeReport/limereport/items/lrchartitemeditor.cpp

333 lines
10 KiB
C++
Raw Normal View History

2016-11-02 01:34:45 +03:00
#include "lrchartitemeditor.h"
#include "ui_lrchartitemeditor.h"
2016-11-02 01:34:45 +03:00
#include "lrchartitem.h"
#include "lrpagedesignintf.h"
2016-11-02 01:34:45 +03:00
#include <QColorDialog>
ChartItemEditor::ChartItemEditor(LimeReport::ChartItem* item, LimeReport::PageDesignIntf* page,
QSettings* settings, QWidget* parent):
QWidget(parent),
ui(new Ui::ChartItemEditor),
m_charItem(item),
m_page(page),
m_settings(settings),
m_ownedSettings(false),
m_isReadingSetting(false)
2016-11-02 01:34:45 +03:00
{
ui->setupUi(this);
QHBoxLayout* colorLayout = new QHBoxLayout();
2021-08-23 08:07:08 +03:00
colorLayout->setContentsMargins(0, 0, 0, 0);
2016-11-02 01:34:45 +03:00
m_colorButton = new QToolButton();
m_colorButton->setText("...");
m_colorButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
2016-11-02 01:34:45 +03:00
m_colorIndicator = new ColorIndicator();
m_colorIndicator->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
2016-11-02 01:34:45 +03:00
ui->colorWidget->setLayout(colorLayout);
colorLayout->addWidget(m_colorIndicator);
colorLayout->addWidget(m_colorButton);
colorLayout->insertStretch(0);
readSetting();
init();
connect(m_colorButton, SIGNAL(clicked(bool)), this, SLOT(slotChangeSeriesColor()));
}
ChartItemEditor::~ChartItemEditor()
{
#ifdef Q_OS_WIN
writeSetting();
#endif
#ifdef Q_OS_MAC
writeSetting();
#endif
delete ui;
}
QSettings* ChartItemEditor::settings()
{
if (m_settings) {
2016-11-02 01:34:45 +03:00
return m_settings;
} else {
m_settings = new QSettings("LimeReport", QCoreApplication::applicationName());
2016-11-02 01:34:45 +03:00
m_ownedSettings = true;
return m_settings;
}
}
void ChartItemEditor::readSetting()
{
if (settings() == 0)
return;
2016-11-02 01:34:45 +03:00
m_isReadingSetting = true;
settings()->beginGroup("ChartItemEditor");
QVariant v = settings()->value("Geometry");
if (v.isValid()) {
2016-11-02 01:34:45 +03:00
restoreGeometry(v.toByteArray());
}
v = settings()->value("State");
if (v.isValid()) {
2016-11-02 01:34:45 +03:00
ui->splitter->restoreState(v.toByteArray());
}
settings()->endGroup();
m_isReadingSetting = false;
}
void ChartItemEditor::writeSetting()
{
if (settings() != 0) {
2016-11-02 01:34:45 +03:00
settings()->beginGroup("ChartItemEditor");
settings()->setValue("Geometry", saveGeometry());
settings()->setValue("State", ui->splitter->saveState());
2016-11-02 01:34:45 +03:00
settings()->endGroup();
}
}
void ChartItemEditor::rebuildTable()
{
ui->tableWidget->clearContents();
ui->tableWidget->setRowCount(m_charItem->series().count());
for (int i = 0; i < m_charItem->series().count(); ++i) {
2016-11-02 01:34:45 +03:00
QTableWidgetItem* newRow = new QTableWidgetItem(m_charItem->series().at(i)->name());
ui->tableWidget->setItem(i, 0, newRow);
2016-11-02 01:34:45 +03:00
}
}
void ChartItemEditor::init()
{
m_initing = true;
ui->tableWidget->setColumnCount(1);
ui->tableWidget->setRowCount(m_charItem->series().count());
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
ui->tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem(tr("Series name")));
2016-11-02 01:34:45 +03:00
rebuildTable();
if (!m_charItem->datasource().isEmpty()) {
if (m_page && m_page->datasourceManager()) {
LimeReport::IDataSource* ds
= m_page->datasourceManager()->dataSource(m_charItem->datasource());
if (ds) {
for (int i = 0; i < ds->columnCount(); ++i) {
ui->valuesFieldComboBox->addItem(ds->columnNameByIndex(i));
ui->labelsFieldComboBox->addItem(ds->columnNameByIndex(i));
ui->xAxisFieldComboBox->addItem(ds->columnNameByIndex(i));
2016-11-02 01:34:45 +03:00
}
}
}
}
2017-09-27 01:05:22 +03:00
static int enumIndex
= LimeReport::SeriesItem::staticMetaObject.indexOfEnumerator("SeriesItemPreferredType");
2017-09-27 01:05:22 +03:00
QMetaEnum enumerator = LimeReport::SeriesItem::staticMetaObject.enumerator(enumIndex);
for (int i = 0; i < enumerator.keyCount(); ++i) {
2017-09-27 01:05:22 +03:00
ui->seriesTypeComboBox->addItem(enumerator.key(i));
}
2021-08-23 08:07:08 +03:00
#if QT_VERSION < 0x050000
ui->labelsFieldComboBox->setCurrentIndex(
ui->labelsFieldComboBox->findText(m_charItem->labelsField()));
ui->xAxisFieldComboBox->setCurrentIndex(
ui->xAxisFieldComboBox->findText(m_charItem->xAxisField()));
2021-08-23 08:07:08 +03:00
#else
ui->labelsFieldComboBox->setCurrentText(m_charItem->labelsField());
ui->xAxisFieldComboBox->setCurrentText(m_charItem->xAxisField());
2017-10-26 12:24:06 +03:00
#endif
if (!m_charItem->series().isEmpty()) {
2016-11-02 01:34:45 +03:00
enableSeriesEditor();
ui->tableWidget->selectRow(0);
} else {
disableSeriesEditor();
}
m_initing = false;
}
void ChartItemEditor::enableSeriesEditor()
{
ui->seriesNameLineEdit->setEnabled(true);
ui->valuesFieldComboBox->setEnabled(true);
2017-09-27 01:05:22 +03:00
ui->seriesTypeComboBox->setEnabled(true);
2016-11-02 01:34:45 +03:00
m_colorButton->setEnabled(true);
m_colorIndicator->setEnabled(true);
}
void ChartItemEditor::disableSeriesEditor()
{
ui->seriesNameLineEdit->setText("");
ui->seriesNameLineEdit->setDisabled(true);
ui->valuesFieldComboBox->setDisabled(true);
m_colorButton->setDisabled(true);
m_colorIndicator->setDisabled(true);
2021-08-23 08:07:08 +03:00
#if QT_VERSION < 0x050000
2017-10-26 12:24:06 +03:00
ui->valuesFieldComboBox->setEditText("");
2021-08-23 08:07:08 +03:00
#else
ui->valuesFieldComboBox->setCurrentText("");
2017-10-26 12:24:06 +03:00
#endif
2017-09-27 01:05:22 +03:00
ui->seriesTypeComboBox->setDisabled(true);
2016-11-02 01:34:45 +03:00
}
LimeReport::SeriesItem* ChartItemEditor::currentSeries()
2016-11-02 01:34:45 +03:00
{
int curRow = ui->tableWidget->currentRow();
if ((curRow > -1) && !m_charItem->series().isEmpty() && m_charItem->series().count() > curRow) {
2016-11-02 01:34:45 +03:00
return m_charItem->series().at(curRow);
}
return 0;
}
void ChartItemEditor::resizeEvent(QResizeEvent*)
2016-11-02 01:34:45 +03:00
{
#ifdef Q_OS_UNIX
writeSetting();
#endif
}
void ChartItemEditor::moveEvent(QMoveEvent*)
2016-11-02 01:34:45 +03:00
{
#ifdef Q_OS_UNIX
writeSetting();
#endif
}
void ChartItemEditor::on_splitter_splitterMoved(int, int)
2016-11-02 01:34:45 +03:00
{
#ifdef Q_OS_UNIX
writeSetting();
#endif
}
void ChartItemEditor::on_pbOk_clicked()
{
emit editingFinished();
2016-11-02 01:34:45 +03:00
close();
}
void ChartItemEditor::slotAddSeries()
{
LimeReport::SeriesItem* series = new LimeReport::SeriesItem();
int curSeriesNumber = m_charItem->series().count();
while (m_charItem->isSeriesExists("Series" + QString::number(curSeriesNumber)))
curSeriesNumber++;
series->setName("Series" + QString::number(curSeriesNumber));
2016-11-02 01:34:45 +03:00
series->setValuesColumn("");
series->setLabelsColumn("");
series->setColor((m_charItem->series().count() < 32)
? LimeReport::color_map[m_charItem->series().count()]
: LimeReport::generateColor());
2016-11-02 01:34:45 +03:00
m_charItem->series().append(series);
ui->tableWidget->setRowCount(m_charItem->series().count());
ui->tableWidget->setItem(m_charItem->series().count() - 1, 0,
new QTableWidgetItem(series->name()));
ui->tableWidget->selectRow(m_charItem->series().count() - 1);
2021-08-23 08:07:08 +03:00
#if QT_VERSION < 0x050000
2017-10-26 12:24:06 +03:00
ui->valuesFieldComboBox->setEditText("");
2021-08-23 08:07:08 +03:00
#else
ui->valuesFieldComboBox->setCurrentText("");
2017-10-26 12:24:06 +03:00
#endif
2016-11-02 01:34:45 +03:00
}
void ChartItemEditor::slotDeleteSeries()
{
QList<LimeReport::SeriesItem*> itemsToRemove;
foreach (QModelIndex index, ui->tableWidget->selectionModel()->selectedRows()) {
itemsToRemove.append(m_charItem->series().at(index.row()));
2016-11-02 01:34:45 +03:00
};
foreach (LimeReport::SeriesItem* series, itemsToRemove) {
2016-11-02 01:34:45 +03:00
m_charItem->series().removeOne(series);
delete series;
}
rebuildTable();
disableSeriesEditor();
}
void ChartItemEditor::on_tableWidget_itemSelectionChanged()
{
if (ui->tableWidget->selectionModel()->hasSelection()) {
LimeReport::SeriesItem* series
= m_charItem->series().at(ui->tableWidget->selectionModel()->currentIndex().row());
2016-11-02 01:34:45 +03:00
ui->seriesNameLineEdit->setText(series->name());
2021-08-23 08:07:08 +03:00
#if QT_VERSION < 0x050000
ui->valuesFieldComboBox->setCurrentIndex(
ui->valuesFieldComboBox->findText(series->valuesColumn()));
2021-08-23 08:07:08 +03:00
#else
ui->valuesFieldComboBox->setCurrentText(series->valuesColumn());
2017-10-26 12:24:06 +03:00
#endif
2016-11-02 01:34:45 +03:00
m_colorIndicator->setColor(series->color());
static int enumIndex
= LimeReport::SeriesItem::staticMetaObject.indexOfEnumerator("SeriesItemPreferredType");
2017-09-27 01:05:22 +03:00
QMetaEnum enumerator = LimeReport::SeriesItem::staticMetaObject.enumerator(enumIndex);
2021-08-23 08:07:08 +03:00
#if QT_VERSION < 0x050000
ui->seriesTypeComboBox->setCurrentIndex(
ui->seriesTypeComboBox->findText(enumerator.valueToKey(series->preferredType())));
2021-08-23 08:07:08 +03:00
#else
ui->seriesTypeComboBox->setCurrentText(enumerator.valueToKey(series->preferredType()));
2017-10-26 12:24:06 +03:00
#endif
2016-11-02 01:34:45 +03:00
enableSeriesEditor();
}
}
void ChartItemEditor::on_seriesNameLineEdit_textChanged(const QString& arg1)
2016-11-02 01:34:45 +03:00
{
if (currentSeries()) {
2016-11-02 01:34:45 +03:00
currentSeries()->setName(arg1);
ui->tableWidget->currentItem()->setText(arg1);
}
}
void ChartItemEditor::on_valuesFieldComboBox_currentTextChanged(const QString& arg1)
2016-11-02 01:34:45 +03:00
{
if (currentSeries()) {
2016-11-02 01:34:45 +03:00
currentSeries()->setValuesColumn(arg1);
}
}
void ChartItemEditor::on_labelsFieldComboBox_currentTextChanged(const QString& arg1)
2016-11-02 01:34:45 +03:00
{
if (!m_initing)
m_charItem->setLabelsField(arg1);
}
void ChartItemEditor::slotChangeSeriesColor()
{
QColorDialog colorDialog;
if (colorDialog.exec()) {
2016-11-02 01:34:45 +03:00
currentSeries()->setColor(colorDialog.selectedColor());
m_colorIndicator->setColor(colorDialog.selectedColor());
}
}
2017-09-27 01:05:22 +03:00
void ChartItemEditor::on_seriesTypeComboBox_currentIndexChanged(const QString& arg1)
2017-09-27 01:05:22 +03:00
{
static int enumIndex
= LimeReport::SeriesItem::staticMetaObject.indexOfEnumerator("SeriesItemPreferredType");
2017-09-27 01:05:22 +03:00
QMetaEnum enumerator = LimeReport::SeriesItem::staticMetaObject.enumerator(enumIndex);
if (currentSeries()) {
currentSeries()->setPreferredType(
static_cast<LimeReport::SeriesItem::SeriesItemPreferredType>(
enumerator.keysToValue(arg1.toLatin1())));
2017-09-27 01:05:22 +03:00
}
}
void ChartItemEditor::on_xAxisFieldComboBox_currentTextChanged(const QString& arg1)
{
if (!m_initing)
m_charItem->setXAxisField(arg1);
2022-03-14 17:49:03 +03:00
}
void ChartItemEditor::on_tableWidget_itemChanged(QTableWidgetItem* item)
2022-03-14 17:49:03 +03:00
{
if (ui->seriesNameLineEdit->hasFocus())
return;
const QString dataStr = item->data(Qt::DisplayRole).toString();
if (dataStr == ui->seriesNameLineEdit->text())
return;
ui->seriesNameLineEdit->setText(dataStr);
}