File indexing completed on 2024-12-22 04:17:31
0001 /*************************************************************************** 0002 * * 0003 * copyright : (C) 2007 The University of Toronto * 0004 * netterfield@astro.utoronto.ca * 0005 * * 0006 * This program is free software; you can redistribute it and/or modify * 0007 * it under the terms of the GNU General Public License as published by * 0008 * the Free Software Foundation; either version 2 of the License, or * 0009 * (at your option) any later version. * 0010 * * 0011 ***************************************************************************/ 0012 0013 #include "filltab.h" 0014 0015 #include <QDebug> 0016 0017 namespace Kst { 0018 0019 FillTab::FillTab(QWidget *parent) 0020 : DialogTab(parent), _multiEdit(false) 0021 { 0022 0023 setupUi(this); 0024 setTabTitle(tr("Fill")); 0025 0026 _style->addItem(tr("No Brush", "Brush type"), (int)Qt::NoBrush); 0027 _style->addItem(tr("Solid Pattern", "Brush type"), (int)Qt::SolidPattern); 0028 _style->addItem(tr("Dense Pattern 1", "Brush type"), (int)Qt::Dense1Pattern); 0029 _style->addItem(tr("Dense Pattern 2", "Brush type"), (int)Qt::Dense2Pattern); 0030 _style->addItem(tr("Dense Pattern 3", "Brush type"), (int)Qt::Dense3Pattern); 0031 _style->addItem(tr("Dense Pattern 4", "Brush type"), (int)Qt::Dense4Pattern); 0032 _style->addItem(tr("Dense Pattern 5", "Brush type"), (int)Qt::Dense5Pattern); 0033 _style->addItem(tr("Dense Pattern 6", "Brush type"), (int)Qt::Dense6Pattern); 0034 _style->addItem(tr("Dense Pattern 7", "Brush type"), (int)Qt::Dense7Pattern); 0035 _style->addItem(tr("Horizontal Pattern", "Brush type"), (int)Qt::HorPattern); 0036 _style->addItem(tr("Vertical Pattern", "Brush type"), (int)Qt::VerPattern); 0037 _style->addItem(tr("Cross Pattern", "Brush type"), (int)Qt::CrossPattern); 0038 _style->addItem(tr("Diagonal Pattern 1", "Brush type"), (int)Qt::BDiagPattern); 0039 _style->addItem(tr("Diagonal Pattern 2", "Brush type"), (int)Qt::FDiagPattern); 0040 _style->addItem(tr("Diagonal Cross Pattern", "Brush type"), (int)Qt::DiagCrossPattern); 0041 0042 connect(_color, SIGNAL(changed(QColor)), this, SIGNAL(modified())); 0043 connect(_style, SIGNAL(currentIndexChanged(int)), this, SIGNAL(modified())); 0044 connect(_gradientEditor, SIGNAL(changed(QGradient)), this, SIGNAL(modified())); 0045 connect(_gradientReset, SIGNAL(pressed()), this, SIGNAL(modified())); 0046 connect(_useGradient, SIGNAL(stateChanged(int)), this, SIGNAL(modified())); 0047 connect(_gradientReset, SIGNAL(pressed()), _gradientEditor, SLOT(resetGradient())); 0048 connect(_useGradient, SIGNAL(stateChanged(int)), this, SLOT(updateButtons())); 0049 0050 updateButtons(); 0051 } 0052 0053 0054 FillTab::~FillTab() { 0055 } 0056 0057 0058 void FillTab::updateButtons() { 0059 if (!_multiEdit) { 0060 _color->setEnabled(!_useGradient->isChecked()); 0061 _style->setEnabled(!_useGradient->isChecked()); 0062 _gradientReset->setEnabled(_useGradient->isChecked()); 0063 _gradientEditor->setEnabled(_useGradient->isChecked()); 0064 } 0065 } 0066 0067 0068 QColor FillTab::color() const { 0069 return _color->color(); 0070 } 0071 0072 0073 void FillTab::setColor(const QColor &color) { 0074 if (color.isValid()) { 0075 _color->setColor(color); 0076 } else { 0077 _color->setColor(Qt::white); 0078 } 0079 } 0080 0081 0082 bool FillTab::colorDirty() const { 0083 return _color->colorDirty(); 0084 } 0085 0086 0087 Qt::BrushStyle FillTab::style() const { 0088 return Qt::BrushStyle(_style->itemData(_style->currentIndex()).toInt()); 0089 } 0090 0091 0092 bool FillTab::styleDirty() const { 0093 return _style->currentIndex() != -1; 0094 } 0095 0096 0097 void FillTab::setStyle(Qt::BrushStyle style) { 0098 if (style == Qt::LinearGradientPattern) { 0099 _style->setCurrentIndex(Qt::SolidPattern); 0100 } else { 0101 _style->setCurrentIndex(_style->findData(QVariant((int)style))); 0102 } 0103 } 0104 0105 0106 QGradient FillTab::gradient() const { 0107 if (_useGradient->isChecked()) { 0108 return _gradientEditor->gradient(); 0109 } else { 0110 return QGradient(); 0111 } 0112 } 0113 0114 0115 bool FillTab::gradientDirty() const { 0116 return _gradientEditor->dirty(); 0117 } 0118 0119 0120 void FillTab::setGradient(const QGradient &gradient) { 0121 _useGradient->setChecked(!gradient.stops().empty()); 0122 _gradientEditor->setGradient(gradient); 0123 updateButtons(); 0124 } 0125 0126 0127 bool FillTab::useGradient() const { 0128 return _useGradient->isChecked(); 0129 } 0130 0131 0132 bool FillTab::useGradientDirty() const { 0133 return _useGradient->checkState() != Qt::PartiallyChecked; 0134 } 0135 0136 0137 void FillTab::setUseGradient(const bool useGradient) { 0138 _useGradient->setChecked(useGradient); 0139 updateButtons(); 0140 } 0141 0142 0143 void FillTab::clearTabValues() { 0144 _useGradient->setCheckState(Qt::PartiallyChecked); 0145 _style->setCurrentIndex(-1); 0146 0147 _color->clearSelection(); 0148 0149 _color->setEnabled(true); 0150 _style->setEnabled(true); 0151 _gradientReset->setEnabled(true); 0152 _gradientEditor->setEnabled(true); 0153 } 0154 0155 0156 void FillTab::enableSingleEditOptions(bool enabled) { 0157 _multiEdit = !enabled; 0158 if (enabled) { 0159 _useGradient->setTristate(false); 0160 } 0161 } 0162 0163 void FillTab::initialize(QBrush *b) { 0164 setColor(b->color()); 0165 setStyle(b->style()); 0166 0167 if (const QGradient *gradient = b->gradient()) { 0168 setGradient(*gradient); 0169 } else { 0170 setUseGradient(false); 0171 } 0172 } 0173 0174 0175 QBrush FillTab::brush(QBrush b) const { 0176 0177 QColor this_color = colorDirty() ? color() : b.color(); 0178 Qt::BrushStyle this_style = styleDirty() ? style() : b.style(); 0179 0180 if (useGradientDirty()) { 0181 // Apply / unapply gradient 0182 if (useGradient()) { 0183 b = QBrush(gradient()); 0184 } else { 0185 b.setColor(this_color); 0186 b.setStyle(this_style); 0187 } 0188 } else { 0189 // Leave gradient but make other changes. 0190 QGradient this_gradient; 0191 if (const QGradient *grad = b.gradient()) { 0192 if (gradientDirty()) { 0193 this_gradient = gradient(); 0194 } else { 0195 this_gradient = *grad; 0196 } 0197 b = QBrush(this_gradient); 0198 } else { 0199 b.setColor(this_color); 0200 b.setStyle(this_style); 0201 } 0202 } 0203 0204 return b; 0205 } 0206 0207 } 0208 0209 // vim: ts=2 sw=2 et