File indexing completed on 2024-05-12 16:33:33

0001 /* This file is part of the KDE project
0002 
0003    Copyright 2007-2008 Johannes Simon <johannes.simon@gmail.com>
0004    Copyright 2009      Inge Wallin    <inge@lysator.liu.se>
0005    Copyright 2018 Dag Andersen <danders@get2net.dk>
0006 
0007    This library is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU Library General Public
0009    License as published by the Free Software Foundation; either
0010    version 2 of the License, or (at your option) any later version.
0011 
0012    This library is distributed in the hope that it will be useful,
0013    but WITHOUT ANY WARRANTY; without even the implied warranty of
0014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015    Library General Public License for more details.
0016 
0017    You should have received a copy of the GNU Library General Public License
0018    along with this library; see the file COPYING.LIB.  If not, write to
0019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020    Boston, MA 02110-1301, USA.
0021 */
0022 
0023 // Own
0024 #include "LegendConfigWidget.h"
0025 #include "ui_LegendConfigWidget.h"
0026 
0027 // KF5
0028 #include <klocalizedstring.h>
0029 #include <KFontChooser>
0030 
0031 // KoChart
0032 #include "Legend.h"
0033 #include "FontEditorDialog.h"
0034 #include "commands/ChartTypeCommand.h"
0035 #include "ChartDebug.h"
0036 
0037 using namespace KoChart;
0038 
0039 
0040 class LegendConfigWidget::Private
0041 {
0042 public:
0043     Private(QWidget *parent);
0044     ~Private();
0045 
0046     Ui::LegendConfigWidget ui;
0047 
0048     FontEditorDialog legendFontEditorDialog;
0049 
0050 };
0051 
0052 
0053 LegendConfigWidget::Private::Private(QWidget *parent)
0054 {
0055 }
0056 
0057 LegendConfigWidget::Private::~Private()
0058 {
0059 }
0060 
0061 
0062 // ================================================================
0063 //                     class LegendConfigWidget
0064 
0065 LegendConfigWidget::LegendConfigWidget()
0066     : d(new Private(this))
0067 {
0068     setObjectName("LegendConfigWidget");
0069     d->ui.setupUi(this);
0070     // FIXME: after 3.2 release: Implement fonts dialog
0071     d->ui.legendEditFontButton->hide();
0072 
0073     connect(d->ui.showLegend, SIGNAL(toggled(bool)),
0074             this,             SIGNAL(showLegendChanged(bool)));
0075 
0076     connect(d->ui.legendTitle, SIGNAL(textChanged(QString)),
0077             this, SIGNAL(legendTitleChanged(QString)));
0078     connect(d->ui.legendOrientation, SIGNAL(activated(int)),
0079             this, SLOT(setLegendOrientation(int)));
0080     connect(d->ui.legendPosition, SIGNAL(activated(int)),
0081             this, SLOT(setLegendPosition(int)));
0082     connect(d->ui.legendAlignment, SIGNAL(activated(int)),
0083             this, SLOT(setLegendAlignment(int)));
0084 
0085     createActions();
0086 }
0087 
0088 LegendConfigWidget::~LegendConfigWidget()
0089 {
0090     delete d;
0091 }
0092 
0093 QAction * LegendConfigWidget::createAction()
0094 {
0095     return 0;
0096 }
0097 
0098 int toIndex(Position pos)
0099 {
0100     int index = 0;
0101     switch (pos) {
0102         case StartPosition: index = 2; break;
0103         case TopPosition: index = 1; break;
0104         case EndPosition: index = 0; break;
0105         case BottomPosition: index = 3; break;
0106         case TopStartPosition: index = 4; break;
0107         case TopEndPosition: index = 5; break;
0108         case BottomStartPosition: index = 6; break;
0109         case BottomEndPosition: index = 7; break;
0110         case CenterPosition:
0111         case FloatingPosition:
0112             index = 8;
0113             break;
0114     }
0115     return index;
0116 }
0117 
0118 int toIndex(Qt::Alignment align)
0119 {
0120     int index = 0;
0121     switch (align) {
0122         case Qt::AlignLeft: index = 0; break;
0123         case Qt::AlignCenter: index = 1; break;
0124         case Qt::AlignRight: index = 2; break;
0125         default:
0126             break;
0127     }
0128     return index;
0129 }
0130 
0131 void LegendConfigWidget::updateData()
0132 {
0133     if (!chart) {
0134         return;
0135     }
0136     if (chart->legend()) {
0137         blockSignals(true);
0138         d->ui.showLegend->setChecked(chart->legend()->isVisible());
0139 
0140         d->ui.legendTitle->setText(chart->legend()->title());
0141 
0142         d->ui.legendOrientation->setCurrentIndex(chart->legend()->expansion());
0143         d->ui.legendPosition->setCurrentIndex(toIndex(chart->legend()->legendPosition()));
0144         d->ui.legendAlignment->setCurrentIndex(toIndex(chart->legend()->alignment()));
0145         blockSignals(false);
0146     }
0147 }
0148 
0149 
0150 void LegendConfigWidget::setLegendOrientation(int boxEntryIndex)
0151 {
0152     emit legendOrientationChanged((Qt::Orientation)boxEntryIndex);
0153 }
0154 /*
0155 void LegendConfigWidget::setLegendShowTitle(bool show)
0156 {
0157     if (show) {
0158         d->ui.legendTitle->setEnabled(true);
0159         emit legendTitleChanged(d->ui.legendTitle->text());
0160     } else {
0161         d->ui.legendTitle->setEnabled(false);
0162         emit legendTitleChanged("");
0163     }
0164 }
0165 */
0166 void LegendConfigWidget::setLegendAlignment(int index)
0167 {
0168     Qt::Alignment align = Qt::AlignCenter;
0169     switch (index) {
0170         case 0: align = Qt::AlignLeft; break;
0171         case 1: align = Qt::AlignCenter; break;
0172         case 2: align = Qt::AlignRight; break;
0173         default:
0174             break;
0175     }
0176     emit legendAlignmentChanged(align);
0177 }
0178 
0179 void LegendConfigWidget::setLegendPosition(int index)
0180 {
0181     Position pos;
0182     switch (index) {
0183         case 0: pos = EndPosition;
0184             break;
0185         case 1: pos = TopPosition;
0186             break;
0187         case 2: pos = BottomPosition;
0188             break;
0189         case 3: pos = StartPosition;
0190             break;
0191         case 4: pos = TopStartPosition;
0192             break;
0193         case 5: pos = TopEndPosition;
0194             break;
0195         case 6: pos = BottomStartPosition;
0196             break;
0197         case 7: pos = BottomEndPosition;
0198             break;
0199         default: // manual
0200             pos = CenterPosition;
0201             break;
0202     }
0203     emit legendPositionChanged(pos);
0204 }
0205 
0206 void LegendConfigWidget::createActions()
0207 {
0208 }
0209 
0210 void LegendConfigWidget::ui_legendEditFontButtonClicked()
0211 {
0212     QFont font = chart->legend()->font();
0213     d->legendFontEditorDialog.fontChooser->setFont(font);
0214     d->legendFontEditorDialog.show();
0215 }