0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-27 09:58:11 +03:00
LimeReport/limereport/items/lrchartitemeditor.cpp

290 lines
9.0 KiB
C++
Raw Normal View History

2016-11-02 01:34:45 +03:00
#include "lrchartitemeditor.h"
#include "ui_lrchartitemeditor.h"
#include "lrchartitem.h"
#include "lrpagedesignintf.h"
#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)
{
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);
m_colorIndicator = new ColorIndicator();
m_colorIndicator->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
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){
return m_settings;
} else {
2017-11-30 21:23:51 +03:00
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;
m_isReadingSetting = true;
settings()->beginGroup("ChartItemEditor");
QVariant v = settings()->value("Geometry");
if (v.isValid()){
restoreGeometry(v.toByteArray());
}
v = settings()->value("State");
if (v.isValid()){
ui->splitter->restoreState(v.toByteArray());
}
settings()->endGroup();
m_isReadingSetting = false;
}
void ChartItemEditor::writeSetting()
{
if (settings()!=0){
settings()->beginGroup("ChartItemEditor");
settings()->setValue("Geometry",saveGeometry());
settings()->setValue("State",ui->splitter->saveState());
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){
QTableWidgetItem* newRow = new QTableWidgetItem(m_charItem->series().at(i)->name());
ui->tableWidget->setItem(i,0,newRow);
}
}
void ChartItemEditor::init()
{
m_initing = true;
ui->tableWidget->setColumnCount(1);
ui->tableWidget->setRowCount(m_charItem->series().count());
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
2017-12-11 00:29:36 +03:00
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));
}
}
}
}
2017-09-27 01:05:22 +03:00
static int enumIndex = LimeReport::SeriesItem::staticMetaObject.indexOfEnumerator("SeriesItemPreferredType");
QMetaEnum enumerator = LimeReport::SeriesItem::staticMetaObject.enumerator(enumIndex);
for (int i = 0; i<enumerator.keyCount(); ++i){
ui->seriesTypeComboBox->addItem(enumerator.key(i));
}
2021-08-23 08:07:08 +03:00
#if QT_VERSION < 0x050000
2017-10-26 12:24:06 +03:00
ui->labelsFieldComboBox->setCurrentIndex(ui->labelsFieldComboBox->findText( m_charItem->labelsField()));
2021-08-23 08:07:08 +03:00
#else
ui->labelsFieldComboBox->setCurrentText(m_charItem->labelsField());
2017-10-26 12:24:06 +03:00
#endif
2016-11-02 01:34:45 +03:00
if (!m_charItem->series().isEmpty()){
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()
{
int curRow = ui->tableWidget->currentRow();
if ((curRow>-1) && !m_charItem->series().isEmpty() && m_charItem->series().count()>curRow){
return m_charItem->series().at(curRow);
}
return 0;
}
void ChartItemEditor::resizeEvent(QResizeEvent *)
{
#ifdef Q_OS_UNIX
writeSetting();
#endif
}
void ChartItemEditor::moveEvent(QMoveEvent *)
{
#ifdef Q_OS_UNIX
writeSetting();
#endif
}
void ChartItemEditor::on_splitter_splitterMoved(int , int )
{
#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));
series->setValuesColumn("");
series->setLabelsColumn("");
series->setColor((m_charItem->series().count()<32)?LimeReport::color_map[m_charItem->series().count()]:LimeReport::generateColor());
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()));
};
foreach (LimeReport::SeriesItem* series, itemsToRemove){
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());
ui->seriesNameLineEdit->setText(series->name());
2021-08-23 08:07:08 +03:00
#if QT_VERSION < 0x050000
2017-10-26 12:24:06 +03:00
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());
2017-09-27 01:05:22 +03:00
static int enumIndex = LimeReport::SeriesItem::staticMetaObject.indexOfEnumerator("SeriesItemPreferredType");
QMetaEnum enumerator = LimeReport::SeriesItem::staticMetaObject.enumerator(enumIndex);
2021-08-23 08:07:08 +03:00
#if QT_VERSION < 0x050000
2017-10-26 12:24:06 +03:00
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)
{
if (currentSeries()){
currentSeries()->setName(arg1);
ui->tableWidget->currentItem()->setText(arg1);
}
}
void ChartItemEditor::on_valuesFieldComboBox_currentTextChanged(const QString &arg1)
{
if (currentSeries()){
currentSeries()->setValuesColumn(arg1);
}
}
void ChartItemEditor::on_labelsFieldComboBox_currentTextChanged(const QString &arg1)
{
if (!m_initing)
m_charItem->setLabelsField(arg1);
}
void ChartItemEditor::slotChangeSeriesColor()
{
QColorDialog colorDialog;
if (colorDialog.exec()){
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)
{
static int enumIndex = LimeReport::SeriesItem::staticMetaObject.indexOfEnumerator("SeriesItemPreferredType");
QMetaEnum enumerator = LimeReport::SeriesItem::staticMetaObject.enumerator(enumIndex);
if (currentSeries()){
currentSeries()->setPreferredType(static_cast<LimeReport::SeriesItem::SeriesItemPreferredType>(enumerator.keysToValue(arg1.toLatin1())));
}
}