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

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 "legenditemdialog.h"
0014 
0015 #include "legendtab.h"
0016 #include "dialogpage.h"
0017 #include "application.h"
0018 #include "objectstore.h"
0019 #include "mainwindow.h"
0020 #include "document.h"
0021 
0022 #include "curve.h"
0023 #include "curvedialog.h"
0024 #include "plotrenderitem.h"
0025 #include "plotitem.h"
0026 #include "image.h"
0027 #include "imagedialog.h"
0028 
0029 namespace Kst {
0030 
0031 LegendItemDialog::LegendItemDialog(LegendItem *item, QWidget *parent)
0032     : ViewItemDialog(item, parent), _legendItem(item) {
0033 
0034   _store = kstApp->mainWindow()->document()->objectStore();
0035 
0036   _legendTab = new LegendTab(this);
0037   connect(_legendTab, SIGNAL(apply()), this, SLOT(legendChanged()));
0038 
0039   DialogPage *page = new DialogPage(this);
0040   page->setPageTitle(tr("Legend"));
0041   page->addDialogTab(_legendTab);
0042   addDialogPage(page);
0043 
0044   setupLegend();
0045 
0046   setSupportsMultipleEdit(true);
0047 
0048   QList<LegendItem *> legends = ViewItem::getItems<LegendItem>();
0049 
0050   clearMultipleEditOptions();
0051   foreach(LegendItem* legend, legends) {
0052     addMultipleEditOption(legend->Name(), legend->descriptionTip(), legend->shortName());
0053   }
0054 
0055   _saveAsDefault->show();
0056   connect(this, SIGNAL(editMultipleMode()), this, SLOT(editMultiple()));
0057   connect(this, SIGNAL(editSingleMode()), this, SLOT(editSingle()));
0058   connect(this, SIGNAL(apply()), this, SLOT(slotApply()));
0059 }
0060 
0061 
0062 LegendItemDialog::~LegendItemDialog() {
0063 }
0064 
0065 void LegendItemDialog::slotApply() {
0066   //FIXME: it is not clear that slotApply must be executed last.
0067   // experimentally, it seems to be...
0068   if (_saveAsDefault->isChecked()) {
0069     _legendItem->saveAsDialogDefaults();
0070   }
0071 }
0072 
0073 
0074 void LegendItemDialog::setupLegend() {
0075   QStringList displayedRelations;
0076   QStringList availableRelations;
0077   QStringList allRelations;
0078   QStringList displayedRelationTips;
0079   QStringList availableRelationTips;
0080   QStringList allRelationTips;
0081 
0082   CurveList curves = _store->getObjects<Curve>();
0083   ImageList images = _store->getObjects<Image>();
0084 
0085   if (_legendItem->relations().isEmpty()) {
0086     foreach (RelationPtr relation, _legendItem->plot()->renderItem(PlotRenderItem::Cartesian)->relationList()) {
0087       displayedRelations.append(relation->Name());
0088       displayedRelationTips.append(relation->descriptionTip());
0089     }
0090   } else {
0091     foreach (RelationPtr relation, _legendItem->relations()) {
0092       displayedRelations.append(relation->Name());
0093       displayedRelationTips.append(relation->descriptionTip());
0094     }
0095   }
0096 
0097   foreach (CurvePtr curve, curves) {
0098     allRelations.append(curve->Name());
0099     allRelationTips.append(curve->descriptionTip());
0100     if (!displayedRelations.contains(curve->Name())) {
0101       availableRelations.append(curve->Name());
0102       availableRelationTips.append(curve->descriptionTip());
0103     }
0104   }
0105 
0106   foreach (ImagePtr image, images) {
0107     allRelations.append(image->Name());
0108     allRelationTips.append(image->descriptionTip());
0109     if (!displayedRelations.contains(image->Name())) {
0110       availableRelations.append(image->Name());
0111       availableRelationTips.append(image->descriptionTip());
0112     }
0113   }
0114 
0115   _legendTab->setDisplayedRelations(displayedRelations, displayedRelationTips);
0116   _legendTab->setAvailableRelations(availableRelations, availableRelationTips);
0117 
0118   _legendTab->setFont(_legendItem->font());
0119   _legendTab->setFontScale(_legendItem->fontScale());
0120   _legendTab->setTitle(_legendItem->title());
0121   _legendTab->setAutoContents(_legendItem->autoContents());
0122   _legendTab->setVerticalDisplay(_legendItem->verticalDisplay());
0123   _legendTab->setColor(_legendItem->legendColor());
0124 
0125 }
0126 
0127 void LegendItemDialog::editMultiple() {
0128   _legendTab->clearTabValues();
0129   _legendTab->setSingle(false);
0130 }
0131 
0132 void LegendItemDialog::editSingle() {
0133   _legendTab->setSingle(true);
0134   setupLegend();
0135 }
0136 
0137 void LegendItemDialog::legendChanged() {
0138   if (editMode() == Multiple) {
0139     foreach(ViewItem* item, selectedMultipleEditObjects()) {
0140       LegendItem* legendItem = (LegendItem*)item;
0141       saveLegend(legendItem, false);
0142     }
0143   } else {
0144     saveLegend(_legendItem, true);
0145   }
0146 }
0147 
0148 void LegendItemDialog::saveLegend(LegendItem *legendItem, bool save_relations) {
0149   legendItem->setLegendFont(_legendTab->font(legendItem->font()));
0150   legendItem->setFontScale(_legendTab->fontScale());
0151   legendItem->setTitle(_legendTab->title());
0152   legendItem->setAutoContents(_legendTab->autoContents());
0153   legendItem->setVerticalDisplay(_legendTab->verticalDisplay());
0154   legendItem->setLegendColor(_legendTab->legendColor());
0155 
0156   QStringList displayedRelations = _legendTab->displayedRelations();
0157 
0158   if (save_relations) {
0159     RelationList newRelations;
0160     foreach (const QString &relationName, displayedRelations) {
0161       if (RelationPtr relation = kst_cast<Relation>(_store->retrieveObject(relationName))) {
0162         newRelations.append(relation);
0163       }
0164     }
0165     legendItem->setRelations(newRelations);
0166   }
0167   legendItem->update();
0168 }
0169 
0170 }
0171 
0172 // vim: ts=2 sw=2 et