File indexing completed on 2024-12-22 04:17:46

0001 /*
0002     themedialog.h: A dialog to set brush, pen, and fonts for view objects.
0003     Copyright (C) 2011  Barth Netterfield <netterfield@astro.utoronto.ca>
0004 
0005     This program is free software; you can redistribute it and/or modify
0006     it under the terms of the GNU General Public License as published by
0007     the Free Software Foundation; either version 2 of the License, or
0008     (at your option) any later version.
0009 
0010     This program is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013     GNU General Public License for more details.
0014 
0015     You should have received a copy of the GNU General Public License along
0016     with this program; if not, write to the Free Software Foundation, Inc.,
0017     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0018 */
0019 
0020 
0021 #include "themedialog.h"
0022 
0023 #include "filltab.h"
0024 #include "stroketab.h"
0025 #include "labelpropertiestab.h"
0026 #include "dialogdefaults.h"
0027 #include "colorsequence.h"
0028 
0029 #include "plotitem.h"
0030 #include "boxitem.h"
0031 #include "circleitem.h"
0032 #include "ellipseitem.h"
0033 #include "arrowitem.h"
0034 #include "labelitem.h"
0035 #include "geticon.h"
0036 
0037 namespace Kst {
0038 
0039 ThemeDialog::ThemeDialog(QWidget *parent) : QDialog(parent) {
0040 
0041   setupUi(this);
0042   setWindowTitle(tr("Theme"));
0043 
0044   _fillTab = new FillTab();
0045   QVBoxLayout *vbox1 = new QVBoxLayout;
0046   vbox1->addWidget(_fillTab);
0047   _fillBox->setLayout(vbox1);
0048 
0049   _strokeTab = new StrokeTab();
0050   QVBoxLayout *vbox2 = new QVBoxLayout;
0051   vbox2->addWidget(_strokeTab);
0052   _strokeBox->setLayout(vbox2);
0053 
0054   connect(_buttonBox, SIGNAL(clicked(QAbstractButton*)),
0055           this, SLOT(buttonClicked(QAbstractButton*)));
0056 
0057   int h = fontMetrics().lineSpacing();
0058 
0059   _bold->setFixedWidth(h);
0060   _bold->setFixedHeight(h);
0061   _bold->setIcon(KstGetIcon("kst_bold"));
0062   _italic->setFixedWidth(h);
0063   _italic->setFixedHeight(h);
0064   _italic->setIcon(KstGetIcon("kst_italic"));
0065   _labelColor->setFixedWidth(h);
0066   _labelColor->setFixedHeight(h);
0067 
0068   // fill color combobox
0069   int color_offset = ColorSequence::self().offset();
0070   ColorSequence::self().setOffset(0);
0071 
0072   for (int i_color = 0; i_color < ColorSequence::self().count(); i_color++) {
0073     QPixmap pm = QPixmap(32,32);
0074     pm.fill(ColorSequence::self().entry(i_color));
0075     QIcon icon = QIcon(pm);
0076     _curveColor->addItem(icon, "");
0077   }
0078   ColorSequence::self().setOffset(color_offset);
0079 
0080   _applyToExisting->setChecked(true);
0081   _saveAsDefault->setChecked(true);
0082 }
0083 
0084 ThemeDialog::~ThemeDialog() {
0085 
0086 }
0087 
0088 void ThemeDialog::reset() {
0089   setFillTab();
0090   setStrokeTab();
0091   setFontTab();
0092 
0093   // reset the curve color combobox
0094   int i_color = dialogDefaults().value("curves/default_color", 0).toInt();
0095   if (i_color > _curveColor->count()) i_color = 0;
0096   _curveColor->setCurrentIndex(i_color);
0097 
0098 }
0099 
0100 void ThemeDialog::apply() {
0101   QBrush b = _fillTab->brush();
0102   QPen p = _strokeTab->pen();
0103 
0104   QStringList typesWithFill;
0105   QStringList typesWithStroke;
0106 
0107   typesWithFill.append(PlotItem::staticDefaultsGroupName());
0108   typesWithStroke.append(PlotItem::staticDefaultsGroupName());
0109 
0110   typesWithFill.append(BoxItem::staticDefaultsGroupName());
0111   typesWithStroke.append(BoxItem::staticDefaultsGroupName());
0112 
0113   typesWithFill.append(CircleItem::staticDefaultsGroupName());
0114   typesWithStroke.append(CircleItem::staticDefaultsGroupName());
0115 
0116   typesWithFill.append(EllipseItem::staticDefaultsGroupName());
0117   typesWithStroke.append(EllipseItem::staticDefaultsGroupName());
0118 
0119   typesWithFill.append(LegendItem::staticDefaultsGroupName());
0120   typesWithStroke.append(LegendItem::staticDefaultsGroupName());
0121 
0122   typesWithStroke.append(LineItem::staticDefaultsGroupName());
0123   typesWithStroke.append(ArrowItem::staticDefaultsGroupName());
0124 
0125   typesWithFill.append(View::staticDefaultsGroupName());
0126 
0127   if (_curveBox->isChecked()) {
0128     ColorSequence::self().setOffset(_curveColor->currentIndex());
0129   }
0130 
0131   if (_saveAsDefault->isChecked()) {
0132     if (_fillBox->isChecked()) {
0133       foreach(const QString &type, typesWithFill) {
0134         saveDialogDefaultsBrush(type,b);
0135       }
0136     }
0137     if (_strokeBox->isChecked()) {
0138       foreach(const QString &type, typesWithStroke) {
0139         saveDialogDefaultsPen(type,p);
0140       }
0141     }
0142 
0143     if (_fontBox->isChecked()) {
0144       QFont F = font();
0145       QColor C = _labelColor->color();
0146       PlotItem::saveDialogDefaultsFont(F, C);
0147       LabelItem::saveDialogDefaultsFont(F, C);
0148       LegendItem::saveDialogDefaultsFont(F, C);
0149     }
0150 
0151     if (_curveBox->isChecked()) {
0152       dialogDefaults().setValue("curves/default_color", _curveColor->currentIndex());
0153     }
0154   }
0155 
0156   if (_applyToExisting->isChecked()) {
0157     QList<ViewItem *> view_items = ViewItem::getItems<ViewItem>();
0158 
0159     foreach (ViewItem *item, view_items) {
0160       if (_strokeBox->isChecked()) {
0161         if (item->hasStroke()) {
0162           item->storePen(p); // fixme: plots don't get repainted
0163         }
0164       }
0165       if (_fillBox->isChecked()) {
0166         if (item->hasBrush()) {
0167           item->setBrush(b);
0168         }
0169       }
0170       if (_fontBox->isChecked()) {
0171         QFont F = font();
0172         QColor C = _labelColor->color();
0173         if (item->hasFont()) {
0174           item->setFont(F,C);
0175         }
0176       }
0177     }
0178     if (_fillBox->isChecked()) {
0179       QList<View*> views = kstApp->mainWindow()->tabWidget()->views();
0180       foreach (View *view, views) {
0181         view->setBackgroundBrush(b);
0182       }
0183     }
0184   }
0185 }
0186 
0187 void ThemeDialog::setFillTab() {
0188   //set the brush
0189   QBrush brush = dialogDefaultsBrush(PlotItem::staticDefaultsGroupName());
0190 
0191   _fillTab->initialize(&brush);
0192 }
0193 
0194 
0195 void ThemeDialog::setStrokeTab() {
0196   // set the pen
0197   QPen pen = dialogDefaultsPen(PlotItem::staticDefaultsGroupName());
0198 
0199   _strokeTab->initialize(&pen);
0200 }
0201 
0202 
0203 void ThemeDialog::setFontTab() {
0204   QFont font;
0205   font.fromString(dialogDefaults().value(PlotItem::staticDefaultsGroupName()+"/globalFontFamily",font.toString()).toString());
0206   QColor color;
0207   color = dialogDefaults().value(PlotItem::staticDefaultsGroupName()+"/globalFontColor", QColor(Qt::black)).value<QColor>();
0208 
0209   _family->setCurrentFont(font);
0210   _bold->setChecked(font.bold());
0211   _italic->setChecked(font.italic());
0212   _labelColor->setColor(color);
0213   _labelFontScale->setValue(dialogDefaults().value(PlotItem::staticDefaultsGroupName()+"/globalFontScale", 13).toDouble());
0214 }
0215 
0216 void ThemeDialog::buttonClicked(QAbstractButton *button) {
0217   QDialogButtonBox::StandardButton std = _buttonBox->standardButton(button);
0218   switch(std) {
0219   case QDialogButtonBox::Ok:
0220     apply();
0221     hide();
0222     break;
0223   case QDialogButtonBox::Apply:
0224     apply();
0225     break;
0226   case QDialogButtonBox::Cancel:
0227     emit cancel();
0228     break;
0229   default:
0230     break;
0231   }
0232 }
0233 
0234 QFont ThemeDialog::font() const {
0235   QFont f(_family->currentFont());
0236   f.setItalic(_italic->isChecked());
0237   f.setBold(_bold->isChecked());
0238   f.setPointSize(_labelFontScale->value());
0239   return f;
0240 }
0241 
0242 }