File indexing completed on 2024-12-22 04:17:31
0001 /*************************************************************************** 0002 * * 0003 * copyright : (C) 2010 C. Barth Netterfield * 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 0014 #include "exportvectorsdialog.h" 0015 #include "dialogdefaults.h" 0016 0017 #include "datavector.h" 0018 #include "objectstore.h" 0019 #include "mainwindow.h" 0020 #include "document.h" 0021 0022 #include <QLineEdit> 0023 0024 namespace Kst { 0025 0026 ExportVectorsDialog::ExportVectorsDialog(QWidget *parent) : 0027 QDialog(parent) 0028 { 0029 setupUi(this); 0030 0031 MainWindow::setWidgetFlags(this); 0032 0033 _saveLocationLabel->setBuddy(_saveLocation->_fileEdit); 0034 _saveLocation->setFile(dialogDefaults().value("vectorexport/filename",QDir::currentPath()).toString()); 0035 0036 if (MainWindow *mw = qobject_cast<MainWindow*>(parent)) { 0037 _store = mw->document()->objectStore(); 0038 } else { 0039 // FIXME: we need the object store 0040 qFatal("ERROR: can't construct a ExportVectorsDialog without the object store"); 0041 } 0042 0043 connect(_add, SIGNAL(clicked()), this, SLOT(addButtonClicked())); 0044 connect(_remove, SIGNAL(clicked()), this, SLOT(removeButtonClicked())); 0045 connect(_removeAll, SIGNAL(clicked()), this, SLOT(removeAll())); 0046 connect(_addAll, SIGNAL(clicked()), this, SLOT(addAll())); 0047 0048 connect(_changeVectorList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(availableDoubleClicked(QListWidgetItem*))); 0049 connect(_selectedVectorList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(selectedDoubleClicked(QListWidgetItem*))); 0050 0051 connect(_changeVectorList, SIGNAL(itemSelectionChanged()), this, SLOT(updateButtons())); 0052 connect(_selectedVectorList, SIGNAL(itemSelectionChanged()), this, SLOT(updateButtons())); 0053 0054 connect(_buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(OKClicked())); 0055 connect(_buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(apply())); 0056 0057 } 0058 0059 ExportVectorsDialog::~ExportVectorsDialog() 0060 { 0061 } 0062 0063 void ExportVectorsDialog::show() { 0064 updateVectorList(); 0065 QDialog::show(); 0066 } 0067 0068 void ExportVectorsDialog::updateVectorList() { 0069 VectorList allVectors = _store->getObjects<Vector>(); 0070 0071 QStringList vectorNameList; 0072 0073 ObjectList<Vector> vectors; 0074 0075 foreach (VectorPtr P, allVectors) { 0076 vectors.append(P); 0077 vectorNameList.append(P->Name()); 0078 } 0079 0080 // make sure all items in _changeVectorList exist in the store; remove if they don't. 0081 for (int i_item = 0; i_item < _changeVectorList->count(); i_item++) { 0082 bool exists=false; 0083 for (int i_vector = 0; i_vector<vectors.count(); i_vector++) { 0084 if (vectors.at(i_vector)->Name() == _changeVectorList->item(i_item)->text()) { 0085 exists = true; 0086 break; 0087 } 0088 } 0089 if (!exists) { 0090 QListWidgetItem *item = _changeVectorList->takeItem(i_item); 0091 delete item; 0092 } 0093 } 0094 0095 // make sure all items in _selectedVectorList exist in the store; remove if they don't. 0096 for (int i_item = 0; i_item < _selectedVectorList->count(); i_item++) { 0097 bool exists=false; 0098 for (int i_vector = 0; i_vector<vectors.count(); i_vector++) { 0099 if (vectors.at(i_vector)->Name() == _selectedVectorList->item(i_item)->text()) { 0100 exists = true; 0101 break; 0102 } 0103 } 0104 if (!exists) { 0105 QListWidgetItem *item = _selectedVectorList->takeItem(i_item); 0106 delete item; 0107 } 0108 } 0109 0110 // insert into _changeVectorList all items in store not in one of the lists. 0111 for (int i_vector = 0; i_vector<vectors.count(); i_vector++) { 0112 bool listed = false; 0113 for (int i_item = 0; i_item<_changeVectorList->count(); i_item++) { 0114 if (vectors.at(i_vector)->Name() == _changeVectorList->item(i_item)->text()) { 0115 _changeVectorList->item(i_item)->setToolTip(vectors.at(i_vector)->descriptionTip()); 0116 listed = true; 0117 break; 0118 } 0119 } 0120 for (int i_item = 0; i_item<_selectedVectorList->count(); i_item++) { 0121 if (vectors.at(i_vector)->Name() == _selectedVectorList->item(i_item)->text()) { 0122 _selectedVectorList->item(i_item)->setToolTip(vectors.at(i_vector)->descriptionTip()); 0123 listed = true; 0124 break; 0125 } 0126 } 0127 if (!listed) { 0128 QListWidgetItem *wi = new QListWidgetItem(vectors.at(i_vector)->Name()); 0129 _changeVectorList->addItem(wi); 0130 wi->setToolTip(vectors.at(i_vector)->descriptionTip()); 0131 } 0132 } 0133 0134 updateButtons(); 0135 } 0136 0137 0138 void ExportVectorsDialog::removeButtonClicked() { 0139 foreach (QListWidgetItem* item, _selectedVectorList->selectedItems()) { 0140 _changeVectorList->addItem(_selectedVectorList->takeItem(_selectedVectorList->row(item))); 0141 } 0142 0143 _changeVectorList->clearSelection(); 0144 updateButtons(); 0145 } 0146 0147 0148 void ExportVectorsDialog::selectedDoubleClicked(QListWidgetItem * item) { 0149 if (item) { 0150 _changeVectorList->addItem(_selectedVectorList->takeItem(_selectedVectorList->row(item))); 0151 _changeVectorList->clearSelection(); 0152 updateButtons(); 0153 } 0154 } 0155 0156 0157 void ExportVectorsDialog::addButtonClicked() { 0158 foreach (QListWidgetItem* item, _changeVectorList->selectedItems()) { 0159 _selectedVectorList->addItem(_changeVectorList->takeItem(_changeVectorList->row(item))); 0160 } 0161 _selectedVectorList->clearSelection(); 0162 updateButtons(); 0163 } 0164 0165 0166 void ExportVectorsDialog::availableDoubleClicked(QListWidgetItem * item) { 0167 if (item) { 0168 _selectedVectorList->addItem(_changeVectorList->takeItem(_changeVectorList->row(item))); 0169 _selectedVectorList->clearSelection(); 0170 updateButtons(); 0171 } 0172 } 0173 0174 void ExportVectorsDialog::addAll() { 0175 _changeVectorList->selectAll(); 0176 addButtonClicked(); 0177 } 0178 0179 0180 void ExportVectorsDialog::removeAll() { 0181 _selectedVectorList->selectAll(); 0182 removeButtonClicked(); 0183 } 0184 0185 void ExportVectorsDialog::updateButtons() { 0186 bool valid = _selectedVectorList->count(); 0187 0188 QFileInfo qfi(_saveLocation->file()); 0189 0190 if (qfi.isFile()) { 0191 valid &= qfi.isWritable(); 0192 } 0193 0194 _buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid); 0195 _buttonBox->button(QDialogButtonBox::Apply)->setEnabled(valid); 0196 0197 _add->setEnabled(_changeVectorList->selectedItems().count() > 0); 0198 _addAll->setEnabled(_changeVectorList->count() > 0); 0199 _remove->setEnabled(_selectedVectorList->selectedItems().count() > 0); 0200 _removeAll->setEnabled(_selectedVectorList->count() > 0); 0201 0202 } 0203 0204 void ExportVectorsDialog::OKClicked() { 0205 if (apply()) { 0206 accept(); 0207 } 0208 } 0209 0210 0211 bool ExportVectorsDialog::apply() { 0212 QFile file(_saveLocation->file()); 0213 VectorList vectors; 0214 QList<int> lengths; 0215 0216 if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) 0217 return false; 0218 0219 QTextStream out(&file); 0220 0221 out << "#"; 0222 int count = _selectedVectorList->count(); 0223 for (int i = 0; i<count; i++) { 0224 VectorPtr V = kst_cast<Vector>(_store->retrieveObject(_selectedVectorList->item(i)->text())); 0225 if (V) { 0226 vectors.append(V); 0227 out << " " << V->descriptiveName(); 0228 lengths << V->length(); 0229 } 0230 } 0231 0232 out << "\n"; 0233 0234 int maxLength = 0; 0235 for (int i=0; i<lengths.size(); i++) { 0236 if (lengths.at(i)>maxLength) { 0237 maxLength = lengths.at(i); 0238 } 0239 } 0240 0241 out.setRealNumberPrecision(14); 0242 0243 int ncols = vectors.size(); 0244 for (int row = 0; row < maxLength; row++) { 0245 for (int col = 0; col < ncols; col++) { 0246 out << " " << vectors.at(col)->interpolate(row, maxLength); 0247 } 0248 out << "\n"; 0249 } 0250 0251 out.flush(); 0252 0253 file.close(); 0254 dialogDefaults().setValue("vectorexport/filename", _saveLocation->file()); 0255 0256 return(true); 0257 } 0258 0259 }