[Kst] branches/work/kst/portto4/kst/src/libkstapp

Mike Fenton mike at staikos.net
Fri Nov 23 17:00:17 CET 2007


SVN commit 740597 by fenton:

Add implementation for ChangeFileDialog.


 M  +169 -24   changefiledialog.cpp  
 M  +7 -1      changefiledialog.h  
 M  +2 -13     changefiledialog.ui  


--- branches/work/kst/portto4/kst/src/libkstapp/changefiledialog.cpp #740596:740597
@@ -15,7 +15,12 @@
 #include "datavector.h"
 #include "datamatrix.h"
 
+#include "objectstore.h"
+#include "document.h"
+#include "mainwindow.h"
+
 #include <QDir>
+#include <QMessageBox>
 
 namespace Kst {
 
@@ -23,20 +28,29 @@
   : QDialog(parent) {
    setupUi(this);
 
+  if (MainWindow *mw = qobject_cast<MainWindow*>(parent)) {
+    _store = mw->document()->objectStore();
+  } else {
+     // FIXME: we need the object store
+    qFatal("ERROR: can't construct a ChangeDataSampleDialog without the object store");
+  }
+
   // TODO Need Icon.
   _clearFilter->setText("Clear Filter");
 
   connect(_changeFileCancel, SIGNAL(clicked()), this, SLOT(reject()));
-  connect(_changeFileOK, SIGNAL(clicked()), this, SLOT(accept()));
-  connect(_changeFileApply, SIGNAL(clicked()), this, SLOT(accept()));
+  connect(_changeFileOK, SIGNAL(clicked()), this, SLOT(OKClicked()));
 
   connect(_changeFileClear, SIGNAL(clicked()), _filter, SLOT(clear()));
-  connect(_changeFileClear, SIGNAL(clicked()), _changeFileCurveList, SLOT(clearSelection()));
+  connect(_changeFileClear, SIGNAL(clicked()), _changeFilePrimitiveList, SLOT(clearSelection()));
   connect(_changeFileSelectAll, SIGNAL(clicked()), _filter, SLOT(clear()));
   connect(_changeFileSelectAll, SIGNAL(clicked()), this, SLOT(selectAll()));
   connect(_clearFilter, SIGNAL(clicked()), _filter, SLOT(clear()));
-  connect(_clearFilter, SIGNAL(clicked()), _changeFileCurveList, SLOT(clearSelection()));
+  connect(_clearFilter, SIGNAL(clicked()), _changeFilePrimitiveList, SLOT(clearSelection()));
 
+  connect(_allFromFile, SIGNAL(clicked()), _filter, SLOT(clear()));
+  connect(_allFromFile, SIGNAL(clicked()), this, SLOT(selectAllFromFile()));
+
   connect(_duplicateSelected, SIGNAL(toggled(bool)), _duplicateDependents, SLOT(setEnabled(bool)));
 
   connect(_filter, SIGNAL(textChanged(const QString&)), this, SLOT(updateSelection(const QString&)));
@@ -52,42 +66,38 @@
 
 
 void ChangeFileDialog::exec() {
-  updateCurveList();
+  updatePrimitiveList();
   QDialog::exec();
 }
 
 
-void ChangeFileDialog::updateCurveList() {
-  DataVectorList dataVectorList; //FIXME //= ObjectSubList<Vector, DataVector>(vectorList);
-  DataMatrixList dataMatrixList; //FIXME //= ObjectSubList<Matrix, DataMatrix>(matrixList);
-  QMap<QString, QString> filesUsed;
+void ChangeFileDialog::updatePrimitiveList() {
+  DataVectorList dataVectorList = _store->getObjects<DataVector>();
+  DataMatrixList dataMatrixList = _store->getObjects<DataMatrix>();
+  QStringList fileNameList;
   int i;
 
-  _changeFileCurveList->clear();
+  _changeFilePrimitiveList->clear();
 
   for (i = 0; i < (int)dataVectorList.count(); i++) {
     dataVectorList[i]->readLock();
-    _changeFileCurveList->addItem(dataVectorList[i]->tag().displayString());
-    filesUsed.insert(dataVectorList[i]->filename(), dataVectorList[i]->filename());
+    _changeFilePrimitiveList->addItem(dataVectorList[i]->tag().displayString());
+    fileNameList.push_back(dataVectorList[i]->filename());
     dataVectorList[i]->unlock();
   }
 
   for (i = 0; i < (int)dataMatrixList.count(); i++) {
     dataMatrixList[i]->readLock();
-    _changeFileCurveList->addItem(dataMatrixList[i]->tag().displayString());
-    filesUsed.insert(dataMatrixList[i]->filename(), dataMatrixList[i]->filename());
+    _changeFilePrimitiveList->addItem(dataMatrixList[i]->tag().displayString());
+    fileNameList.push_back(dataMatrixList[i]->filename());
     dataMatrixList[i]->unlock();
   }
 
   QString currentFile = _files->currentText();
   _files->clear();
 
-//  dataSourceList.lock();
-  DataSourceList dataSourceList;  // FIXME
-  for (DataSourceList::Iterator it = dataSourceList.begin(); it != dataSourceList.end(); ++it) {
-    if (filesUsed.contains((*it)->fileName())) {
-      _files->addItem((*it)->fileName());
-    }
+  for (QStringList::Iterator it = fileNameList.begin(); it != fileNameList.end(); ++it) {
+    _files->addItem(*it);
   }
 
   _allFromFile->setEnabled(_files->count() > 0);
@@ -96,18 +106,153 @@
 
 
 void ChangeFileDialog::selectAll() {
-  _changeFileCurveList->selectAll();
+  _changeFilePrimitiveList->selectAll();
 }
 
 
 void ChangeFileDialog::updateSelection(const QString& txt) {
-  _changeFileCurveList->clearSelection();
+  _changeFilePrimitiveList->clearSelection();
   QRegExp re(txt, Qt::CaseSensitive, QRegExp::Wildcard);
-  for (int i = 0; i < _changeFileCurveList->count(); ++i) {
-    _changeFileCurveList->item(i)->setSelected(re.exactMatch(_changeFileCurveList->item(i)->text()));
+  for (int i = 0; i < _changeFilePrimitiveList->count(); ++i) {
+    _changeFilePrimitiveList->item(i)->setSelected(re.exactMatch(_changeFilePrimitiveList->item(i)->text()));
   }
 }
 
+
+void ChangeFileDialog::selectAllFromFile() {
+  if (_files->count() <= 0) {
+    return;
+  }
+
+  _changeFilePrimitiveList->clearSelection();
+
+  for (int i = 0; i < _changeFilePrimitiveList->count(); i++) {
+    if (DataVectorPtr vector = kst_cast<DataVector>(_store->retrieveObject(ObjectTag::fromString(_changeFilePrimitiveList->item(i)->text())))) {
+      _changeFilePrimitiveList->item(i)->setSelected(vector->filename() == _files->currentText());
+    } else if (DataMatrixPtr matrix = kst_cast<DataMatrix>(_store->retrieveObject(ObjectTag::fromString(_changeFilePrimitiveList->item(i)->text())))) {
+      _changeFilePrimitiveList->item(i)->setSelected(matrix->filename() == _files->currentText());
+    }
+  }
 }
 
+
+void ChangeFileDialog::OKClicked() {
+  Q_ASSERT(_store);
+  DataSourcePtr dataSource = DataSource::findOrLoadSource(_store, _dataFile->file());
+  if (!dataSource || !dataSource->isValid() || dataSource->isEmpty()) {
+    QMessageBox::critical(this, tr("Kst"), tr("The file could not be loaded or contains no data."), QMessageBox::Ok);
+    reject();
+  }
+
+  DataSourceList oldSources;
+  QString invalidSources;
+  int invalid = 0;
+
+  QMap<DataVectorPtr, DataVectorPtr> duplicatedVectors;
+  QMap<DataMatrixPtr, DataMatrixPtr> duplicatedMatrices;
+
+  QList<QListWidgetItem*> selectedItems = _changeFilePrimitiveList->selectedItems();
+  for (int i = 0; i < selectedItems.size(); ++i) {
+    if (DataVectorPtr vector = kst_cast<DataVector>(_store->retrieveObject(ObjectTag::fromString(selectedItems[i]->text())))) {
+      vector->writeLock();
+      dataSource->readLock();
+      bool valid = dataSource->isValidField(vector->field());
+      dataSource->unlock();
+      if (!valid) {
+        if (invalid > 0) {
+          invalidSources += ", ";
+          }
+        invalidSources = vector->field();
+        ++invalid;
+      } else {
+        if (_duplicateSelected->isChecked()) {
+          DataVectorPtr newVector = _store->createObject<DataVector>(vector->tag());
+
+          newVector->writeLock();
+          newVector->change(dataSource, vector->field(),
+              vector->startFrame(),
+              vector->numFrames(),
+              vector->skip(),
+              vector->doSkip(),
+              vector->doAve());
+
+          newVector->update(0);
+          newVector->unlock();
+
+          if (_duplicateDependents->isChecked()) {
+//            FIXME:  Associated code required not currently implemented.
+//             duplicatedVectors.insert(vector, newVector);
+//             KST::duplicateDependents(KstVectorPtr(vector), duplicatedMap, duplicatedVectors);
+          }
+        } else {
+          if (!oldSources.contains(vector->dataSource())) {
+            oldSources << vector->dataSource();
+          }
+          vector->changeFile(dataSource);
+        }
+      }
+      vector->unlock();
+    } else if (DataMatrixPtr matrix = kst_cast<DataMatrix>(_store->retrieveObject(ObjectTag::fromString(selectedItems[i]->text())))) {
+      matrix->writeLock();
+      dataSource->readLock();
+      bool valid = dataSource->isValidMatrix(matrix->field());
+      dataSource->unlock();
+      if (!valid) {
+        if (invalid > 0) {
+          invalidSources += ", ";
+          }
+        invalidSources = matrix->field();
+        ++invalid;
+      } else {
+        if (_duplicateSelected->isChecked()) {
+          DataMatrixPtr newMatrix = _store->createObject<DataMatrix>(matrix->tag());
+
+          newMatrix->writeLock();
+          newMatrix->change(dataSource, matrix->field(),
+              matrix->reqXStart(),
+              matrix->reqYStart(),
+              matrix->reqXNumSteps(),
+              matrix->reqYNumSteps(),
+              matrix->doSkip(),
+              matrix->skip(),
+              matrix->doAverage());
+
+          newMatrix->update(0);
+          newMatrix->unlock();
+
+          if (_duplicateDependents->isChecked()) {
+//          FIXME:  Associated code required not currently implemented.
+//             duplicatedMatrices.insert(KstMatrixPtr(matrix), KstMatrixPtr(newMatrix));
+//             KST::duplicateDependents(KstMatrixPtr(matrix), duplicatedMap, duplicatedMatrices);
+          }
+        } else {
+          if (!oldSources.contains(matrix->dataSource())) {
+            oldSources << matrix->dataSource();
+          }
+          matrix->changeFile(dataSource);
+        }
+      }
+      matrix->unlock();
+    }
+  }
+
+  // clean up unused data sources
+  for (DataSourceList::Iterator it = oldSources.begin(); it != oldSources.end(); ++it) {
+    if ((*it)->getUsage() == 1) {
+      _store->removeObject(*it);
+    }
+  }
+
+  if (!invalidSources.isEmpty()) {
+    if (invalid == 1) {
+      QMessageBox::warning(this, tr("Kst"), tr("The following field is not defined for the requested file:\n%1").arg(invalidSources), QMessageBox::Ok);
+    } else {
+      QMessageBox::warning(this, tr("Kst"), tr("The following fields are not defined for the requested file:\n%1").arg(invalidSources), QMessageBox::Ok);
+    }
+  }
+  accept();
+}
+
+}
+
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/changefiledialog.h #740596:740597
@@ -20,6 +20,8 @@
 
 namespace Kst {
 
+class ObjectStore;
+
 class KST_EXPORT ChangeFileDialog : public QDialog, Ui::ChangeFileDialog
 {
   Q_OBJECT
@@ -32,9 +34,13 @@
   private Q_SLOTS:
     void selectAll();
     void updateSelection(const QString&);
+    void selectAllFromFile();
+    void OKClicked();
 
   private:
-    void updateCurveList();
+    void updatePrimitiveList();
+
+    ObjectStore *_store;
 };
 
 }
--- branches/work/kst/portto4/kst/src/libkstapp/changefiledialog.ui #740596:740597
@@ -100,7 +100,7 @@
          <number>0</number>
         </property>
         <item rowspan="5" row="0" column="0" >
-         <widget class="QListWidget" name="_changeFileCurveList" >
+         <widget class="QListWidget" name="_changeFilePrimitiveList" >
           <property name="sizePolicy" >
            <sizepolicy vsizetype="Expanding" hsizetype="Expanding" >
             <horstretch>1</horstretch>
@@ -383,16 +383,6 @@
       </spacer>
      </item>
      <item>
-      <widget class="QPushButton" name="_changeFileApply" >
-       <property name="whatsThis" >
-        <string>Apply new source file to selected vectors.</string>
-       </property>
-       <property name="text" >
-        <string>&amp;Apply</string>
-       </property>
-      </widget>
-     </item>
-     <item>
       <widget class="QPushButton" name="_changeFileOK" >
        <property name="whatsThis" >
         <string>Apply new source file to selected vectors.</string>
@@ -438,14 +428,13 @@
   <tabstop>_files</tabstop>
   <tabstop>_clearFilter</tabstop>
   <tabstop>_filter</tabstop>
-  <tabstop>_changeFileCurveList</tabstop>
+  <tabstop>_changeFilePrimitiveList</tabstop>
   <tabstop>_changeFileClear</tabstop>
   <tabstop>_changeFileSelectAll</tabstop>
   <tabstop>_dataFile</tabstop>
   <tabstop>_changeSelected</tabstop>
   <tabstop>_duplicateSelected</tabstop>
   <tabstop>_duplicateDependents</tabstop>
-  <tabstop>_changeFileApply</tabstop>
   <tabstop>_changeFileCancel</tabstop>
  </tabstops>
  <resources/>


More information about the Kst mailing list