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

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 "viewvectordialog.h"
0014 
0015 #include "document.h"
0016 #include "vectormodel.h"
0017 #include "editmultiplewidget.h"
0018 #include "updateserver.h"
0019 #include "geticon.h"
0020 #include "dialogdefaults.h"
0021 
0022 #include <datacollection.h>
0023 #include <objectstore.h>
0024 #include <QHeaderView>
0025 #include <QMenu>
0026 
0027 #ifdef QT5
0028 #define setResizeMode setSectionResizeMode
0029 #endif
0030 
0031 namespace Kst {
0032 
0033 ViewVectorDialog::ViewVectorDialog(QWidget *parent, Document *doc)
0034   : QDialog(parent), _doc(doc) {
0035   _model = 0;
0036 
0037   Q_ASSERT(_doc && _doc->objectStore());
0038   setupUi(this);
0039 
0040   int size = style()->pixelMetric(QStyle::PM_SmallIconSize);
0041   _showVectorList->setFixedSize(size + 8, size + 8);
0042   _hideVectorList->setFixedSize(size + 8, size + 8);
0043 
0044   _vectors->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
0045   // Allow reorganizing the columns per drag&drop
0046   _vectors->horizontalHeader()->setMovable(true);
0047 
0048   // Custom context menu for the remove action and display format
0049   setContextMenuPolicy(Qt::CustomContextMenu);
0050   connect(this, SIGNAL(customContextMenuRequested(QPoint)),
0051           this, SLOT(contextMenu(QPoint)));
0052 
0053   connect(_resetButton, SIGNAL(clicked()), this, SLOT(reset()));
0054 
0055   // Add vector list, reusing the editmultiplewidget class + some tweaking
0056   _showMultipleWidget = new EditMultipleWidget();
0057   if (_showMultipleWidget) {
0058     // Set header
0059     _showMultipleWidget->setHeader(tr("Select Vectors to View"));
0060     // Populate the list
0061     update();
0062     // Finish setting up the layout
0063     _vectorListLayout->addWidget(_showMultipleWidget);
0064   }
0065   // Add/remove buttons
0066   _addButton = new QPushButton();
0067   _addButton->setIcon(KstGetIcon("kst_rightarrow"));
0068   _addButton->setShortcut(tr("Alt+S"));
0069   _addButton->setToolTip(tr("View selected vector(s)"));
0070   _addButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
0071   _removeButton = new QPushButton();
0072   _removeButton->setIcon(KstGetIcon("kst_leftarrow"));
0073   _removeButton->setShortcut(tr("Alt+R"));
0074   _removeButton->setToolTip(tr("Remove selected vector(s) from view"));
0075   _removeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
0076   _addRemoveLayout->addStretch();
0077   _addRemoveLayout->addWidget(_addButton);
0078   _addRemoveLayout->addWidget(_removeButton);
0079   _addRemoveLayout->addStretch();
0080 
0081   _splitter->setStretchFactor(0,0);
0082   _splitter->setStretchFactor(1,1);
0083   _splitter->setCollapsible(1, false);
0084   _splitterSizes = _splitter->sizes();
0085   connect(_addButton, SIGNAL(clicked()), this, SLOT(addSelected()));
0086   connect(_removeButton, SIGNAL(clicked()), this, SLOT(removeSelected()));
0087   connect(_showMultipleWidget, SIGNAL(itemDoubleClicked()), this, SLOT(addSelected()));
0088   connect(_showVectorList, SIGNAL(clicked()), this, SLOT(showVectorList()));
0089   connect(_hideVectorList, SIGNAL(clicked()), this, SLOT(hideVectorList()));
0090 
0091   connect(UpdateServer::self(), SIGNAL(objectListsChanged()), this, SLOT(update()));
0092 
0093 }
0094 
0095 
0096 ViewVectorDialog::~ViewVectorDialog() {
0097   delete _model;
0098   delete _showMultipleWidget;
0099   delete _addButton;
0100   delete _removeButton;
0101 }
0102 
0103 void ViewVectorDialog::show() {
0104   // vectorSelected();
0105   QDialog::show();
0106 }
0107 
0108 void ViewVectorDialog::contextMenu(const QPoint& position) {
0109   QMenu menu;
0110   QPoint cursor = QCursor::pos();
0111   QAction* removeAction = menu.addAction(tr("Remove"));
0112   // Add submenu to select nb of digits
0113   QMenu* submenu = new QMenu(tr("Significant digits"));
0114   QAction* digitNb0Action = submenu->addAction(tr("Show as int"));
0115   QAction* digitNb3Action = submenu->addAction("3");
0116   QAction* digitNb6Action = submenu->addAction(tr("6"));
0117   QAction* digitNb12Action = submenu->addAction("12");
0118   QAction* digitNb17Action = submenu->addAction("17");
0119   menu.addMenu(submenu);
0120   QAction* selectedItem = menu.exec(cursor);
0121   int digits = 6;
0122   if (selectedItem == removeAction) {
0123     removeSelected();
0124     return;
0125   } else if (selectedItem == digitNb0Action) {
0126       digits = 0;
0127   } else if (selectedItem == digitNb3Action) {
0128       digits = 3;
0129   } else if (selectedItem == digitNb6Action) {
0130       digits = 6;
0131   } else if (selectedItem == digitNb12Action) {
0132       digits = 12;
0133   } else if (selectedItem == digitNb17Action) {
0134       digits = 17;
0135   } else {
0136       return;
0137   }
0138   foreach (int column, selectedColumns()) {
0139     _model->setDigitNumber(column, digits);
0140   }
0141   dialogDefaults().setValue("viewvector/digits", digits);
0142 
0143   _vectors->resizeColumnsToContents();
0144 }
0145 
0146 void ViewVectorDialog::update()
0147 {
0148   VectorList objects = _doc->objectStore()->getObjects<Vector>();
0149   _showMultipleWidget->clearObjects();
0150   foreach(VectorPtr object, objects) {
0151     _showMultipleWidget->addObject(object->Name(), object->descriptionTip());
0152   }
0153   if (_model) {
0154     _model->resetIfChanged();
0155     _vectors->viewport()->update();
0156   }
0157 }
0158 
0159 void ViewVectorDialog::addSelected() {
0160   if (_model == 0) {
0161       _model = new VectorModel();
0162       _vectors->setModel(_model);
0163   }
0164   // Retrieve list of selected objects by name
0165   QStringList objects = _showMultipleWidget->selectedObjects();
0166   // Get to the pointers and add them to the model
0167   foreach (const QString &objectName, objects) {
0168     VectorPtr vector = kst_cast<Vector>(_doc->objectStore()->retrieveObject(objectName));
0169     if (vector) {
0170       _model->addVector(vector);
0171     }
0172   }
0173   _vectors->resizeColumnsToContents();
0174 }
0175 
0176 void ViewVectorDialog::removeSelected() {
0177   if (!_model) {
0178     return;
0179   }
0180   // Remove columns starting from the highest index to avoid shifting them
0181   int column;
0182   foreach (column, selectedColumns()) {
0183     _model->removeVector(column);
0184   }
0185 }
0186 
0187 void ViewVectorDialog::reset() {
0188   delete _model;
0189   _model = 0;
0190 }
0191 
0192 void ViewVectorDialog::showVectorList() {
0193   _splitterSizes[0] = qMax(_splitterSizes[0],150);
0194   _splitter->setSizes(_splitterSizes);
0195 }
0196 
0197 void ViewVectorDialog::hideVectorList() {
0198   _splitterSizes = _splitter->sizes();
0199   QList<int> sizes;
0200   sizes << 0 << width();
0201   _splitter->setSizes(sizes);
0202 }
0203 
0204 QList<int> ViewVectorDialog::selectedColumns() {
0205   // Get current selection
0206   QModelIndexList sel = _vectors->selectionModel()->selectedIndexes();
0207   // Now go through the list to see how may columns it spans
0208   QList<int> columns;
0209   QModelIndex index;
0210   foreach (index, sel) {
0211     if (!columns.contains(index.column())) {
0212       columns << index.column();
0213     }
0214   }
0215   // Sort the columns in descending order
0216   qSort(columns.begin(), columns.end(), qGreater<int>());
0217   return columns;
0218 }
0219 
0220 
0221 }
0222 
0223 
0224 // vim: ts=2 sw=2 et