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

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 "differentiatecurvesdialog.h"
0014 #include "linestyle.h"
0015 #include "curve.h"
0016 #include "colorsequence.h"
0017 #include "geticon.h"
0018 
0019 #include "objectstore.h"
0020 #include "mainwindow.h"
0021 #include "document.h"
0022 #include "application.h"
0023 #include "updatemanager.h"
0024 
0025 namespace Kst {
0026 
0027 DifferentiateCurvesDialog::DifferentiateCurvesDialog(QWidget *parent)
0028   : QDialog(parent) {
0029    setupUi(this);
0030 
0031   if (MainWindow *mw = qobject_cast<MainWindow*>(parent)) {
0032     _store = mw->document()->objectStore();
0033   } else {
0034     // FIXME: we need the object store
0035     qFatal("ERROR: can't construct a DifferentiateCurvesDialog without the object store");
0036   }
0037 
0038   resetLists();
0039 
0040   connect(_buttonBox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), this, SLOT(reject()));
0041   connect(_buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(OKClicked()));
0042   connect(_buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(apply()));
0043 
0044   connect(_add, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
0045   connect(_remove, SIGNAL(clicked()), this, SLOT(removeButtonClicked()));
0046   connect(_up, SIGNAL(clicked()), this, SLOT(upButtonClicked()));
0047   connect(_down, SIGNAL(clicked()), this, SLOT(downButtonClicked()));
0048   connect(_availableListBox, SIGNAL(itemSelectionChanged()), this, SLOT(updateButtons()));
0049   connect(_selectedListBox, SIGNAL(itemSelectionChanged()), this, SLOT(updateButtons()));
0050   connect(_availableListBox, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(addButtonClicked()));
0051   connect(_selectedListBox, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(removeButtonClicked()));
0052 
0053   // Use the standard icons
0054   _up->setIcon(KstGetIcon("kst_uparrow"));
0055   _down->setIcon(KstGetIcon("kst_downarrow"));
0056   _add->setIcon(KstGetIcon("kst_rightarrow"));
0057   _remove->setIcon(KstGetIcon("kst_leftarrow"));
0058   _up->setToolTip(tr("Move the selected property up the priority list"));
0059   _down->setToolTip(tr("Move the selected property down the priority list"));
0060   _add->setToolTip(tr("Add the selected property to the list of properties to cycle through"));
0061   _remove->setToolTip(tr("Remove the selected property from the list of properties to cycle through"));
0062 
0063   _maxLineWidth->setMaximum(LINEWIDTH_MAX);
0064 }
0065 
0066 
0067 DifferentiateCurvesDialog::~DifferentiateCurvesDialog() {
0068 }
0069 
0070 
0071 void DifferentiateCurvesDialog::show() {
0072   updateButtons();
0073   QDialog::show();
0074 }
0075 
0076 
0077 void DifferentiateCurvesDialog::resetLists() {
0078   _availableListBox->clear();
0079   _selectedListBox->clear();
0080   _availableListBox->addItem(tr("Line Color"));
0081   _availableListBox->addItem(tr("Point Style"));
0082   _availableListBox->addItem(tr("Line Style"));
0083   _availableListBox->addItem(tr("Line Width"));
0084 
0085   _maxLineWidth->setValue(1);
0086   _pointDensity->setCurrentIndex(0);
0087 }
0088 
0089 
0090 void DifferentiateCurvesDialog::updateButtons() {
0091 
0092   QList<QListWidgetItem *> selectedItems = _selectedListBox->selectedItems();
0093   QListWidgetItem *selectedItem = 0;
0094 
0095   if (selectedItems.count() > 0)
0096     selectedItem = selectedItems.first();
0097 
0098   _remove->setEnabled(selectedItems.count() > 0);
0099 
0100   _up->setEnabled(_selectedListBox->row(selectedItem) > 0);
0101   _down->setEnabled(_selectedListBox->row(selectedItem) >= 0 && _selectedListBox->row(selectedItem) < (int)_selectedListBox->count() - 1);
0102 
0103   _add->setEnabled(_availableListBox->selectedItems().count() > 0);
0104 
0105   _buttonBox->button(QDialogButtonBox::Apply)->setEnabled(_selectedListBox->selectedItems().count() > 0);
0106 }
0107 
0108 
0109 void DifferentiateCurvesDialog::removeButtonClicked() {
0110   for (int i = 0; i < _selectedListBox->count(); i++) {
0111     if (_selectedListBox->item(i) && _selectedListBox->item(i)->isSelected()) {
0112       _availableListBox->addItem(_selectedListBox->takeItem(i));
0113       _availableListBox->clearSelection();
0114       _availableListBox->item(_availableListBox->count() - 1)->setSelected(true);
0115     }
0116   }
0117   updateButtons();
0118 }
0119 
0120 
0121 void DifferentiateCurvesDialog::addButtonClicked() {
0122   for (int i = 0; i < _availableListBox->count(); i++) {
0123     if (_availableListBox->item(i) && _availableListBox->item(i)->isSelected()) {
0124       _selectedListBox->addItem(_availableListBox->takeItem(i));
0125       _selectedListBox->clearSelection();
0126       _selectedListBox->item(_selectedListBox->count() - 1)->setSelected(true);
0127     }
0128   }
0129   updateButtons();
0130 }
0131 
0132 
0133 void DifferentiateCurvesDialog::upButtonClicked() {
0134   int i = _selectedListBox->currentRow();
0135   if (i != -1) {
0136     QListWidgetItem *item = _selectedListBox->takeItem(i);
0137     _selectedListBox->insertItem(i-1, item);
0138     _selectedListBox->clearSelection();
0139     item->setSelected(true);
0140     updateButtons();
0141   }
0142 }
0143 
0144 
0145 void DifferentiateCurvesDialog::downButtonClicked() {
0146   // move item down
0147   int i = _selectedListBox->currentRow();
0148   if (i != -1) {
0149     QListWidgetItem *item = _selectedListBox->takeItem(i);
0150     _selectedListBox->insertItem(i+1, item);
0151     _selectedListBox->clearSelection();
0152     item->setSelected(true);
0153     updateButtons();
0154   }
0155 }
0156 
0157 
0158 void DifferentiateCurvesDialog::OKClicked() {
0159   if (_buttonBox->button(QDialogButtonBox::Apply)->isEnabled()) {
0160     apply();
0161   }
0162   accept();
0163 }
0164 
0165 
0166 void DifferentiateCurvesDialog::apply() {
0167   bool lineColorOrder  = !_selectedListBox->findItems(tr("Line Color"), Qt::MatchExactly).empty();
0168   bool pointStyleOrder = !_selectedListBox->findItems(tr("Point Style"), Qt::MatchExactly).empty();
0169   bool lineStyleOrder  = !_selectedListBox->findItems(tr("Line Style"), Qt::MatchExactly).empty();
0170   bool lineWidthOrder  = !_selectedListBox->findItems(tr("Line Width"), Qt::MatchExactly).empty();
0171 
0172   int maxLineWidth = _maxLineWidth->value();
0173   int pointDensity = _pointDensity->currentIndex();
0174 
0175   int sequenceNum = 0;
0176   CurveList curveList = _store->getObjects<Curve>();
0177   for (CurveList::iterator curve_iter = curveList.begin(); curve_iter != curveList.end(); ++curve_iter)
0178   {
0179     CurvePtr curve = kst_cast<Curve>(*curve_iter);
0180     curve->writeLock();
0181     if (lineColorOrder) {
0182       curve->setColor(ColorSequence::self().entry(sequenceNum));
0183     }
0184     if (pointStyleOrder) {
0185       curve->setPointType(sequenceNum % KSTPOINT_MAXTYPE);
0186       curve->setHasPoints(true);
0187       curve->setPointDensity(pointDensity);
0188     }
0189     if (lineStyleOrder) {
0190       curve->setLineStyle(sequenceNum % LINESTYLE_MAXTYPE);
0191     }
0192     if (lineWidthOrder) {
0193       curve->setLineWidth((sequenceNum + 1) % maxLineWidth);
0194     }
0195 
0196     curve->registerChange();
0197     curve->unlock();
0198     ++sequenceNum;
0199   }
0200   resetLists();
0201 
0202   UpdateManager::self()->doUpdates(true);
0203   kstApp->mainWindow()->document()->setChanged(true);
0204 }
0205 
0206 }
0207 
0208 // vim: ts=2 sw=2 et