90 changes: 64 additions & 26 deletions src/libkstapp/viewvectordialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "document.h"
#include "vectormodel.h"
#include "editmultiplewidget.h"

#include <datacollection.h>
#include <objectstore.h>
Expand Down Expand Up @@ -43,17 +44,41 @@ ViewVectorDialog::ViewVectorDialog(QWidget *parent, Document *doc)
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(contextMenu(const QPoint&)));

connect(_vectorSelector, SIGNAL(selectionChanged(const QString&)), this, SLOT(vectorSelected()));
connect(_resetButton, SIGNAL(clicked()), this, SLOT(reset()));

_vectorSelector->setObjectStore(doc->objectStore());

// Add vector list, reusing the editmultiplewidget class + some tweaking
_showMultipleWidget = new EditMultipleWidget();
QPushButton *addButton = new QPushButton("+");
QPushButton *removeButton = new QPushButton("-");
if (_showMultipleWidget) {
// Set header
_showMultipleWidget->setHeader(i18n("Select Vectors to View"));
// Populate the list
VectorList objects = _doc->objectStore()->getObjects<Vector>();
_showMultipleWidget->clearObjects();
foreach(VectorPtr object, objects) {
_showMultipleWidget->addObject(object->Name(), object->descriptionTip());
}
// Finish setting up the layout
_listLayout->addWidget(_showMultipleWidget,0,0,Qt::AlignLeft);
QVBoxLayout *addRemoveButtons = new QVBoxLayout();
addRemoveButtons->addStretch();
addRemoveButtons->addWidget(addButton);
addRemoveButtons->addWidget(removeButton);
addRemoveButtons->addStretch();
_listLayout->addLayout(addRemoveButtons,0,1);
}
_splitter->setStretchFactor(0,0);
_splitter->setStretchFactor(1,1);
connect(addButton, SIGNAL(clicked()), this, SLOT(addSelected()));
connect(removeButton, SIGNAL(clicked()), this, SLOT(removeSelected()));
// setAttribute(Qt::WA_DeleteOnClose);
}


ViewVectorDialog::~ViewVectorDialog() {
delete _model;
delete _showMultipleWidget;
}


Expand All @@ -69,42 +94,55 @@ void ViewVectorDialog::contextMenu(const QPoint& position)
QAction* removeAction = menu.addAction(tr("Remove"));
QAction* selectedItem = menu.exec(cursor);
if (selectedItem == removeAction) {
// Get current selection
QModelIndexList sel = _vectors->selectionModel()->selectedIndexes();
// Now go through the list to see how may columns it spans
QList<int> columns;
QModelIndex index;
foreach (index, sel) {
if (!columns.contains(index.column())) {
columns << index.column();
}
}
// Sort the columns in descending order
qSort(columns.begin(), columns.end(), qGreater<int>());
// Remove columns starting from the highest index to avoid shifting them
int column;
foreach (column, columns) {
_model->removeVector(column);
}
removeSelected();
}
}


void ViewVectorDialog::vectorSelected() {
void ViewVectorDialog::addSelected() {
if (_model == 0) {
_model = new VectorModel(_vectorSelector->selectedVector());
_model = new VectorModel();
_vectors->setModel(_model);
} else {
_model->addVector(_vectorSelector->selectedVector());
}
// Retrieve list of selected objects by name
QStringList objects = _showMultipleWidget->selectedObjects();
// Get to the pointers and add them to the model
foreach (const QString &objectName, objects) {
VectorPtr vector = kst_cast<Vector>(_doc->objectStore()->retrieveObject(objectName));
if (vector) {
_model->addVector(vector);
}
}
}

void ViewVectorDialog::removeSelected() {
if (!_model) {
return;
}
// Get current selection
QModelIndexList sel = _vectors->selectionModel()->selectedIndexes();
// Now go through the list to see how may columns it spans
QList<int> columns;
QModelIndex index;
foreach (index, sel) {
if (!columns.contains(index.column())) {
columns << index.column();
}
}
// Sort the columns in descending order
qSort(columns.begin(), columns.end(), qGreater<int>());
// Remove columns starting from the highest index to avoid shifting them
int column;
foreach (column, columns) {
_model->removeVector(column);
}
}

void ViewVectorDialog::reset() {
delete _model;
_model = 0;

}


}

// vim: ts=2 sw=2 et
15 changes: 9 additions & 6 deletions src/libkstapp/viewvectordialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Kst {

class Document;
class VectorModel;
class EditMultipleWidget;

class ViewVectorDialog : public QDialog, Ui::ViewVectorDialog
{
Expand All @@ -37,12 +38,14 @@ public Q_SLOTS:
void contextMenu(const QPoint& position);

private Q_SLOTS:
void vectorSelected();
void reset();

private:
Document *_doc;
VectorModel *_model;
void addSelected();
void removeSelected();
void reset();

private:
Document *_doc;
VectorModel *_model;
EditMultipleWidget *_showMultipleWidget;
};

}
Expand Down
137 changes: 74 additions & 63 deletions src/libkstapp/viewvectordialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,82 +6,93 @@
<rect>
<x>0</x>
<y>0</y>
<width>528</width>
<width>468</width>
<height>536</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>View Vector Values</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Select:</string>
</property>
</widget>
</item>
<item>
<widget class="Kst::VectorSelector" name="_vectorSelector" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QTableView" name="_vectors">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Select a vector from the drop-down menu to add it to the view.&lt;/p&gt;&lt;p&gt;You can remove vectors from the view using the context menu (right click).&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<widget class="QSplitter" name="_splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
<widget class="QWidget" name="gridLayoutWidget">
<layout class="QGridLayout" name="_listLayout">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="_resetButton">
<property name="text">
<string>Reset View</string>
<property name="rightMargin">
<number>4</number>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="_closeButton">
<property name="text">
<string>Close</string>
</property>
</widget>
</item>
</layout>
</layout>
</widget>
<widget class="QWidget" name="layoutWidget">
<layout class="QVBoxLayout" name="_viewLayout">
<item>
<widget class="QTableView" name="_vectors">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>450</width>
<height>450</height>
</size>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="_resetButton">
<property name="text">
<string>Reset View</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="_closeButton">
<property name="text">
<string>Close</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
<zorder>splitter</zorder>
<zorder>gridLayoutWidget</zorder>
<zorder>_vectors</zorder>
</widget>
<customwidgets>
<customwidget>
<class>Kst::VectorSelector</class>
<extends>QWidget</extends>
<header>vectorselector.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
Expand Down