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

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2008 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 "legendtab.h"
0014 #include "objectstore.h"
0015 #include "geticon.h"
0016 
0017 #include <qdebug.h>
0018 
0019 namespace Kst {
0020 
0021 LegendTab::LegendTab(QWidget *parent)
0022   : DialogTab(parent) {
0023 
0024   setupUi(this);
0025 
0026   _single = true;
0027 
0028   _up->setIcon(KstGetIcon("kst_uparrow"));
0029   _down->setIcon(KstGetIcon("kst_downarrow"));
0030   _add->setIcon(KstGetIcon("kst_rightarrow"));
0031   _remove->setIcon(KstGetIcon("kst_leftarrow"));
0032   _up->setToolTip(tr("Raise in list order: Alt+Up"));
0033   _down->setToolTip(tr("Lower in list order: Alt+Down"));
0034   _add->setToolTip(tr("Select: Alt+s"));
0035   _remove->setToolTip(tr("Remove: Alt+r"));
0036 
0037   int h = fontMetrics().lineSpacing();
0038   _bold->setFixedWidth(h);
0039   _bold->setFixedHeight(h);
0040   _bold->setIcon(KstGetIcon("kst_bold"));
0041   _italic->setFixedWidth(h);
0042   _italic->setFixedHeight(h);
0043   _italic->setIcon(KstGetIcon("kst_italic"));
0044   _labelColor->setFixedWidth(h);
0045   _labelColor->setFixedHeight(h);
0046 
0047 
0048   connect(_add, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
0049   connect(_remove, SIGNAL(clicked()), this, SLOT(removeButtonClicked()));
0050   connect(_up, SIGNAL(clicked()), this, SLOT(upButtonClicked()));
0051   connect(_down, SIGNAL(clicked()), this, SLOT(downButtonClicked()));
0052 
0053   connect(_add, SIGNAL(clicked()), this, SIGNAL(modified()));
0054   connect(_remove, SIGNAL(clicked()), this, SIGNAL(modified()));
0055   connect(_up, SIGNAL(clicked()), this, SIGNAL(modified()));
0056   connect(_down, SIGNAL(clicked()), this, SIGNAL(modified()));
0057 
0058   connect(_availableRelationList, SIGNAL(itemSelectionChanged()), this, SLOT(updateButtons()));
0059   connect(_displayedRelationList, SIGNAL(itemSelectionChanged()), this, SLOT(updateButtons()));
0060   connect(_availableRelationList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(addButtonClicked()));
0061   connect(_displayedRelationList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(removeButtonClicked()));
0062 
0063   connect(_autoContents, SIGNAL(stateChanged(int)), this, SLOT(updateActive()));
0064   connect(_autoContents, SIGNAL(stateChanged(int)), this, SIGNAL(modified()));
0065   connect(_displayVertically, SIGNAL(stateChanged(int)), this, SIGNAL(modified()));
0066 
0067   connect(_title, SIGNAL(textChanged(QString)), this, SIGNAL(modified()));
0068   connect(_fontSize, SIGNAL(valueChanged(double)), this, SIGNAL(modified()));
0069   connect(_bold, SIGNAL(toggled(bool)), this, SIGNAL(modified()));
0070   connect(_italic, SIGNAL(toggled(bool)), this, SIGNAL(modified()));
0071   connect(_labelColor, SIGNAL(changed(QColor)), this, SIGNAL(modified()));
0072   connect(_family, SIGNAL(currentIndexChanged(int)), this, SIGNAL(modified()));
0073 
0074   _displayedRelationList->setSortingEnabled(false);
0075   _availableRelationList->setSortingEnabled(false);
0076 }
0077 
0078 
0079 LegendTab::~LegendTab() {
0080 }
0081 
0082 
0083 void LegendTab::updateActive() {
0084   _contentGroupBox->setEnabled((!_autoContents->isChecked()) && _single);
0085 }
0086 
0087 
0088 void LegendTab::updateButtons() {
0089 
0090   QList<QListWidgetItem *> displayedItems = _displayedRelationList->selectedItems();
0091   QListWidgetItem *displayedItem = 0;
0092 
0093   if (displayedItems.count() > 0)
0094     displayedItem = displayedItems.first();
0095 
0096   _remove->setEnabled(displayedItems.count() > 0);
0097 
0098   _up->setEnabled(_displayedRelationList->row(displayedItem) > 0);
0099   _down->setEnabled(_displayedRelationList->row(displayedItem) >= 0 && _displayedRelationList->row(displayedItem) < (int)_displayedRelationList->count() - 1);
0100 
0101   _add->setEnabled(_availableRelationList->selectedItems().count() > 0);
0102 }
0103 
0104 
0105 void LegendTab::removeButtonClicked() {
0106   for (int i = 0; i < _displayedRelationList->count(); i++) {
0107     if (_displayedRelationList->item(i) && _displayedRelationList->item(i)->isSelected()) {
0108       _availableRelationList->addItem(_displayedRelationList->takeItem(i));
0109       _availableRelationList->clearSelection();
0110       _availableRelationList->item(_availableRelationList->count() - 1)->setSelected(true);
0111     }
0112   }
0113   updateButtons();
0114 }
0115 
0116 
0117 void LegendTab::addButtonClicked() {
0118   for (int i = 0; i < _availableRelationList->count(); i++) {
0119     if (_availableRelationList->item(i) && _availableRelationList->item(i)->isSelected()) {
0120       _displayedRelationList->addItem(_availableRelationList->takeItem(i));
0121       _displayedRelationList->clearSelection();
0122       _displayedRelationList->item(_displayedRelationList->count() - 1)->setSelected(true);
0123     }
0124   }
0125   updateButtons();
0126 }
0127 
0128 
0129 void LegendTab::upButtonClicked() {
0130   //_displayedRelationList->setFocus();
0131 
0132   for (int i=1; i<_displayedRelationList->count(); i++) {
0133     if (_displayedRelationList->item(i) && _displayedRelationList->item(i)->isSelected()) {
0134       QListWidgetItem *item = _displayedRelationList->takeItem(i);
0135       _displayedRelationList->insertItem(i-1, item);
0136       item->setSelected(true);
0137     }
0138   }
0139   updateButtons();
0140 }
0141 
0142 
0143 void LegendTab::downButtonClicked() {
0144 
0145   for (int i=_displayedRelationList->count()-2; i>=0; --i) {
0146     if (_displayedRelationList->item(i) && _displayedRelationList->item(i)->isSelected()) {
0147       QListWidgetItem *item = _displayedRelationList->takeItem(i);
0148       _displayedRelationList->insertItem(i+1, item);
0149       item->setSelected(true);
0150     }
0151   }
0152   updateButtons();
0153 
0154 }
0155 
0156 
0157 void LegendTab::setDisplayedRelations(const QStringList& displayedRelations, const QStringList& displayedRelationTips) {
0158   _displayedRelationList->clear();
0159   _displayedRelationList->addItems(displayedRelations);
0160   for (int i=0; i<_displayedRelationList->count(); i++) {
0161     _displayedRelationList->item(i)->setToolTip(displayedRelationTips.at(i));
0162   }
0163 }
0164 
0165 
0166 void LegendTab::setAvailableRelations(const QStringList& availableRelations, const QStringList& availableRelationTips) {
0167   _availableRelationList->clear();
0168   _availableRelationList->addItems(availableRelations);
0169   for (int i=0; i<_availableRelationList->count(); i++) {
0170     _availableRelationList->item(i)->setToolTip(availableRelationTips.at(i));
0171   }
0172 }
0173 
0174 
0175 QStringList LegendTab::displayedRelations() {
0176   QStringList relations;
0177   for (int i = 0; i < _displayedRelationList->count(); i++) {
0178     relations.append(_displayedRelationList->item(i)->text());
0179   }
0180   return relations;
0181 }
0182 
0183 QFont LegendTab::font(QFont ref_font) const {
0184   QString family = (_family->currentIndex() == -1) ?
0185                    ref_font.family() : _family->currentFont().family();
0186   QFont font(family);
0187   font.setItalic(_italic->isChecked());
0188   font.setBold(_bold->isChecked());
0189   return font;
0190 }
0191 
0192 //FIXME: handle tristate bool/italics, etc.
0193 bool LegendTab::fontDirty() const {
0194   return true;
0195 }
0196 
0197 void LegendTab::setFont(const QFont &font) {
0198   _family->setCurrentFont(font);
0199   _bold->setChecked(font.bold());
0200   _italic->setChecked(font.italic());
0201 }
0202 
0203 
0204 qreal LegendTab::fontScale() const {
0205   return _fontSize->value();
0206 }
0207 
0208 
0209 void LegendTab::setFontScale(const qreal scale) {
0210   _fontSize->setValue(scale);
0211 }
0212 
0213 bool LegendTab::fontScaleDirty() const {
0214   return _fontSize->text().isEmpty();
0215 }
0216 
0217 bool LegendTab::autoContents() const {
0218   return _autoContents->isChecked();
0219 }
0220 
0221 
0222 void LegendTab::setAutoContents(const bool autoContents) {
0223   _autoContents->setChecked(autoContents);
0224 }
0225 
0226 bool LegendTab::autoContentsDirty() const {
0227   return _autoContents->checkState() != Qt::PartiallyChecked;
0228 }
0229 
0230 bool LegendTab::verticalDisplay() const {
0231   return _displayVertically->isChecked();
0232 }
0233 
0234 
0235 void LegendTab::setVerticalDisplay(const bool vertical) {
0236   _displayVertically->setChecked(vertical);
0237 }
0238 
0239 bool LegendTab::verticalDisplayDirty() const {
0240   return _displayVertically->checkState() != Qt::PartiallyChecked;
0241 }
0242 
0243 
0244 QString LegendTab::title() const {
0245   return _title->text();
0246 }
0247 
0248 
0249 void LegendTab::setTitle(const QString& title) {
0250   _title->setText(title);
0251 }
0252 
0253 bool LegendTab::titleDirty() const {
0254   return _title->text().isEmpty();
0255 }
0256 
0257 void LegendTab::clearTabValues() {
0258   _autoContents->setCheckState(Qt::PartiallyChecked);
0259   _fontSize->clear();
0260   _family->setCurrentIndex(-1);
0261   _displayVertically->setCheckState(Qt::PartiallyChecked);
0262 }
0263 
0264 void LegendTab::setSingle(bool single) {
0265   _single = single;
0266   updateActive();
0267   if (single) {
0268     _autoContents->setTristate(false);
0269     _displayVertically->setTristate(false);
0270   }
0271 }
0272 
0273 }
0274 
0275 // vim: ts=2 sw=2 et