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

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 "contenttab.h"
0014 
0015 #include "objectstore.h"
0016 #include "relation.h"
0017 #include "dialoglauncher.h"
0018 #include "geticon.h"
0019 
0020 #include <qdebug.h>
0021 
0022 namespace Kst {
0023 
0024 ContentTab::ContentTab(QWidget *parent, ObjectStore *store)
0025   : DialogTab(parent), _store(store) {
0026 
0027   setupUi(this);
0028 
0029   _up->setIcon(KstGetIcon("kst_uparrow"));
0030   _down->setIcon(KstGetIcon("kst_downarrow"));
0031   _add->setIcon(KstGetIcon("kst_rightarrow"));
0032   _remove->setIcon(KstGetIcon("kst_leftarrow"));
0033   _up->setToolTip(tr("Raise in plot order: Alt+Up"));
0034   _down->setToolTip(tr("Lower in plot order: Alt+Down"));
0035   _add->setToolTip(tr("Select: Alt+s"));
0036   _remove->setToolTip(tr("Remove: Alt+r"));
0037 
0038 
0039   connect(_add, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
0040   connect(_remove, SIGNAL(clicked()), this, SLOT(removeButtonClicked()));
0041   connect(_up, SIGNAL(clicked()), this, SLOT(upButtonClicked()));
0042   connect(_down, SIGNAL(clicked()), this, SLOT(downButtonClicked()));
0043 
0044   connect(_add, SIGNAL(clicked()), this, SIGNAL(modified()));
0045   connect(_remove, SIGNAL(clicked()), this, SIGNAL(modified()));
0046   connect(_up, SIGNAL(clicked()), this, SIGNAL(modified()));
0047   connect(_down, SIGNAL(clicked()), this, SIGNAL(modified()));
0048 
0049   connect(_availableRelationList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(availableDoubleClicked(QListWidgetItem*)));
0050   connect(_displayedRelationList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(displayedDoubleClicked(QListWidgetItem*)));
0051 
0052   connect(_availableRelationList, SIGNAL(itemSelectionChanged()), this, SLOT(updateButtons()));
0053   connect(_displayedRelationList, SIGNAL(itemSelectionChanged()), this, SLOT(updateButtons()));
0054 
0055   connect(_editSelectedAvailable, SIGNAL(clicked()), this, SLOT(editSelectedAvailable()));
0056   connect(_editSelectedDisplayed, SIGNAL(clicked()), this, SLOT(editSelectedDisplayed()));
0057   connect(_curveSearchEntry, SIGNAL(textChanged(QString)), this, SLOT(filterCurves(QString)));
0058   connect(_curveSearch, SIGNAL(clicked()), this, SLOT(searchCurves()));
0059 
0060 }
0061 
0062 
0063 ContentTab::~ContentTab() {
0064 }
0065 
0066 void ContentTab::editSelectedAvailable() {
0067   QList<QListWidgetItem *> selected = _availableRelationList->selectedItems();
0068 
0069   if (selected.count()>1) {
0070     QList<ObjectPtr> objects;
0071     int n = selected.count();
0072     for (int i=0; i<n; i++) {
0073       objects.append(_store->retrieveObject(selected.at(i)->text()));
0074     }
0075     DialogLauncher::self()->showMultiObjectDialog(objects);
0076   } else if (selected.count() > 0) {
0077     QString name = selected.at(0)->text();
0078     RelationPtr relation = kst_cast<Relation>(_store->retrieveObject(name));
0079     DialogLauncher::self()->showObjectDialog(relation);
0080   }
0081 }
0082 
0083 void ContentTab::editSelectedDisplayed() {
0084   QList<QListWidgetItem *> selected = _displayedRelationList->selectedItems();
0085 
0086 
0087   if (selected.count()>1) {
0088     QList<ObjectPtr> objects;
0089     int n = selected.count();
0090     for (int i=0; i<n; i++) {
0091       objects.append(_store->retrieveObject(selected.at(i)->text()));
0092     }
0093     DialogLauncher::self()->showMultiObjectDialog(objects);
0094   } else if (selected.count() > 0) {
0095     QString name = selected.at(0)->text();
0096     RelationPtr relation = kst_cast<Relation>(_store->retrieveObject(name));
0097     DialogLauncher::self()->showObjectDialog(relation);
0098   }
0099 }
0100 
0101 void ContentTab::updateButtons() {
0102 
0103   QList<QListWidgetItem *> displayedItems = _displayedRelationList->selectedItems();
0104   QListWidgetItem *displayedItem = 0;
0105 
0106   if (displayedItems.count() > 0)
0107     displayedItem = displayedItems.first();
0108 
0109   _remove->setEnabled(displayedItems.count() > 0);
0110 
0111   _up->setEnabled(_displayedRelationList->row(displayedItem) > 0);
0112   _down->setEnabled(_displayedRelationList->row(displayedItem) >= 0 && _displayedRelationList->row(displayedItem) < (int)_displayedRelationList->count() - 1);
0113 
0114   _add->setEnabled(_availableRelationList->selectedItems().count() > 0);
0115 }
0116 
0117 
0118 void ContentTab::removeButtonClicked() {
0119   foreach (QListWidgetItem* item, _displayedRelationList->selectedItems()) {
0120     _availableRelationList->addItem(_displayedRelationList->takeItem(_displayedRelationList->row(item)));
0121   }
0122 
0123   _availableRelationList->clearSelection();
0124   updateButtons();
0125 }
0126 
0127 
0128 void ContentTab::displayedDoubleClicked(QListWidgetItem * item) {
0129   if (item) {
0130     _availableRelationList->addItem(_displayedRelationList->takeItem(_displayedRelationList->row(item)));
0131     _availableRelationList->clearSelection();
0132     emit modified();
0133     updateButtons();
0134   }
0135 }
0136 
0137 
0138 void ContentTab::addButtonClicked() {
0139   foreach (QListWidgetItem* item, _availableRelationList->selectedItems()) {
0140     _displayedRelationList->addItem(_availableRelationList->takeItem(_availableRelationList->row(item)));
0141   }
0142   _displayedRelationList->clearSelection();
0143   updateButtons();
0144 }
0145 
0146 
0147 void ContentTab::availableDoubleClicked(QListWidgetItem * item) {
0148   if (item) {
0149     _displayedRelationList->addItem(_availableRelationList->takeItem(_availableRelationList->row(item)));
0150     _displayedRelationList->clearSelection();
0151     emit modified();
0152     updateButtons();
0153   }
0154 }
0155 
0156 
0157 void ContentTab::upButtonClicked() {
0158   _displayedRelationList->setFocus();
0159 
0160   int i = _displayedRelationList->currentRow();
0161   if (i != -1) {
0162     QListWidgetItem *item = _displayedRelationList->takeItem(i);
0163     _displayedRelationList->insertItem(i-1, item);
0164     _displayedRelationList->clearSelection();
0165     _displayedRelationList->setCurrentItem(item);
0166     //item->setSelected(true);
0167     updateButtons();
0168   }
0169 }
0170 
0171 
0172 void ContentTab::downButtonClicked() {
0173   _displayedRelationList->setFocus();
0174   // move item down
0175   int i = _displayedRelationList->currentRow();
0176   if (i != -1) {
0177     QListWidgetItem *item = _displayedRelationList->takeItem(i);
0178     _displayedRelationList->insertItem(i+1, item);
0179     _displayedRelationList->clearSelection();
0180     _displayedRelationList->setCurrentItem(item);
0181     //item->setSelected(true);
0182     updateButtons();
0183   }
0184 }
0185 
0186 
0187 void ContentTab::setDisplayedRelations(QStringList displayedRelations, QStringList displayedRelationTips) {
0188   _displayedRelationList->clear();
0189   _displayedRelationList->addItems(displayedRelations);
0190   for (int i=0; i<_displayedRelationList->count(); i++) {
0191     _displayedRelationList->item(i)->setToolTip(displayedRelationTips.at(i));
0192   }
0193 }
0194 
0195 
0196 void ContentTab::setAvailableRelations(QStringList availableRelations, QStringList availableRelationTips) {
0197   _availableRelationList->clear();
0198   _availableRelationList->addItems(availableRelations);
0199   for (int i=0; i<_availableRelationList->count(); i++) {
0200     _availableRelationList->item(i)->setToolTip(availableRelationTips.at(i));
0201   }
0202 }
0203 
0204 
0205 QStringList ContentTab::displayedRelations() {
0206   QStringList relations;
0207   for (int i = 0; i < _displayedRelationList->count(); i++) {
0208     relations.append(_displayedRelationList->item(i)->text());
0209   }
0210   return relations;
0211 }
0212 
0213 void ContentTab::addObject(QString x) {
0214     for(int i=0;i<_availableRelationList->count();i++) {
0215         if(_availableRelationList->item(i)->text().contains(x)) {
0216             _displayedRelationList->addItem(_availableRelationList->takeItem(i));
0217             return;
0218         }
0219     }
0220 }
0221 
0222 void ContentTab::removeObject(QString x) {
0223     for(int i=0;i<_displayedRelationList->count();i++) {
0224         if(_displayedRelationList->item(i)->text().contains(x)) {
0225             _availableRelationList->addItem(_displayedRelationList->takeItem(i));
0226             return;
0227         }
0228     }
0229 }
0230 
0231 void ContentTab::filterCurves(const QString &filter) {
0232   _availableRelationList->clearSelection();
0233 
0234   if (filter=="*") { // optimization
0235     _availableRelationList->selectAll();
0236     return;
0237   }
0238 
0239   QRegExp re(filter, Qt::CaseSensitive, QRegExp::Wildcard);
0240   QStringList selected;
0241 
0242   for (int i = 0; i < _availableRelationList->count(); i++) {
0243     QListWidgetItem *item = _availableRelationList->item(i);
0244     if (re.exactMatch(item->text())) {
0245       item = _availableRelationList->takeItem(i);
0246       selected.append(item->text());
0247       i--;
0248     }
0249   }
0250 
0251   _availableRelationList->insertItems(0, selected);
0252 
0253   // special case optimization:
0254   // selecting and unselecting individual items is expensive,
0255   // but selecting all of them is fast,
0256   // so either select or select all, then unselect, which ever is fewer.
0257   if (selected.count() > _availableRelationList->count()/2) {
0258     _availableRelationList->selectAll();
0259     for (int i=selected.count(); i<_availableRelationList->count(); i++) {
0260       _availableRelationList->item(i)->setSelected(false);
0261     }
0262   } else {
0263     for (int i=0; i<selected.count(); i++) {
0264       _availableRelationList->item(i)->setSelected(true);
0265     }
0266   }
0267 
0268   if (selected.count()>0) {
0269     _availableRelationList->scrollToTop();
0270   }
0271 
0272 }
0273 
0274 void ContentTab::searchCurves() {
0275   QString s = _curveSearchEntry->text();
0276   if (!s.isEmpty()) {
0277     if (s[0] != '*') {
0278       s = '*' + s;
0279     }
0280     if (s[s.length()-1] != '*') {
0281       s += '*';
0282     }
0283     _curveSearchEntry->setText(s);
0284   }
0285 }
0286 
0287 }
0288 
0289 // vim: ts=2 sw=2 et