File indexing completed on 2024-04-21 14:43:51

0001 /*************************************************************************************
0002  *  Copyright (C) 2010-2012 by Percy Camilo T. Aucahuasi <percy.camilo.ta@gmail.com> *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (at your option) any later version.                           *
0008  *                                                                                   *
0009  *  This program is distributed in the hope that it will be useful,                  *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012  *  GNU General Public License for more details.                                     *
0013  *                                                                                   *
0014  *  You should have received a copy of the GNU General Public License                *
0015  *  along with this program; if not, write to the Free Software                      *
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017  *************************************************************************************/
0018 
0019 #include "dashboard.h"
0020 #include "ui_dashboard.h"
0021 
0022 //Analitza includes
0023 #include <analitza/variables.h>
0024 #include <analitzaplot/plotsmodel.h>
0025 
0026 //Qt includes
0027 #include <QAbstractItemView>
0028 #include <QStringListModel>
0029 #include <QMainWindow>
0030 #include <QPainter>
0031 #include <QRadialGradient>
0032 #include <QPaintEvent>
0033 #include <QToolButton>
0034 #include <QMenu>
0035 #include <QDebug>
0036 #include <QClipboard>
0037 
0038 //KDE includes
0039 #include <kpushbutton.h>
0040 #include <klineedit.h>
0041 #include <kwidgetitemdelegate.h>
0042 #include <KSelectionProxyModel>
0043 #include <QFileDialog>
0044 #include <KStandardDirs>
0045 #include <KMessageBox>
0046 #include <KNS3/DownloadDialog>
0047 
0048 //local includes
0049 #include "spaceinformation.h"
0050 #include "spaceoptions.h"
0051 #include "spacesmodel.h"
0052 #include "datastore.h"
0053 #include "spaceplotsproxymodel.h"
0054 #include "spaceitem.h"
0055 #include "spacesdelegate.h"
0056 
0057 using namespace Analitza;
0058 
0059 Dashboard::Dashboard(QWidget *parent)
0060     : QStackedWidget(parent)
0061     , m_ui(new Ui::DashboardWidget)
0062 {
0063     m_ui->setupUi(this);
0064     m_openclicked=false;
0065 
0066     m_filterText << "Dimension-All" << "Dimension-2D" << "Dimension-3D";
0067     m_ui->comboBox->clear();
0068     initializeDictionaryNames();
0069     initializePlotsViews();
0070 
0071     //Init plotters
0072     m_ui->plotsView2D->setKeepAspectRatio(true);
0073     m_ui->plotsView2D->setAutoGridStyle(false);
0074     m_ui->plotsView2D->setViewport(QRectF(QPointF(-5.0, 5.0), QSizeF(10.0, -10.0)));
0075     m_ui->plotsView2D->setShowMinorTicks(true);
0076     m_ui->plotsView2D->setShowMinorGrid(true);
0077     m_ui->plotsView2D->setShowPolarAxis(true);
0078     m_ui->plotsView2D->setShowPolarAngles(true);
0079     
0080     //Init space delegate and proxyfilter
0081     m_spacesProxyModel = new SpacesFilterProxyModel(this);
0082     m_delegate = new SpacesDelegate(m_ui->spacesView, this);
0083     
0084     m_ui->spacesView->setItemDelegate(m_delegate);
0085     
0086     connect(m_ui->spacesView, SIGNAL(doubleClicked(QModelIndex)), SLOT(setCurrentSpace(QModelIndex)));
0087 }
0088 
0089 Dashboard::~Dashboard()
0090 {
0091 }
0092 
0093 void Dashboard::initializeDictionaryNames()
0094 {
0095     // QString str=KStandardDirs::installPath("data"); // works for -DCMAKE_INSTALL_PREFIX=`kde4-config --prefix`
0096     // QDir dir(str.append("libanalitza/plots"));  // path to the dir where .plots files are installed by analitza
0097 
0098     KStandardDirs KStdDirs;
0099     QStringList dirList = KStdDirs.findDirs("data","libanalitza");
0100 
0101     QStringList filters("*.plots");
0102 
0103     foreach(const QString &dirPath, dirList) {
0104         QDir dir=QString(dirPath).append("plots");
0105         dir.setNameFilters(filters);
0106         QString key=dir.path().append("/"); // dir path , goes into key section of the map
0107         QStringList fileList=dir.entryList();
0108         foreach(const QString &file, fileList) {
0109             m_DictionaryPathName.insertMulti(key,file);
0110             m_ui->comboBox->addItem(QFileInfo(file).baseName());
0111         }
0112     }
0113 
0114     connect(m_ui->comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(setDictionaryData(int)));
0115 
0116     if(m_DictionaryPathName.empty() || m_DictionaryPathName.keys().empty()) {
0117         QString error =i18n("Please make sure that the dictionary (.plots) files are correctly installed");
0118         KMessageBox::error(this,error,i18n("No Dictionary found!"));
0119         return;
0120     }
0121 }
0122 
0123 void Dashboard::initializePlotsViews()
0124 {
0125     m_ui->plotsView2D->setShowGrid(false);
0126 }
0127 
0128 void Dashboard::setDocument(DataStore* doc)
0129 {
0130     m_delegate->hideOperatorBar();
0131     m_delegate->setDocument(doc);
0132     
0133     //NOTE avoid re-connection when the doc is updated (see mainapp::loadfile)
0134     if (doc != m_document)
0135         connect(m_delegate, SIGNAL(saveDictionary(QModelIndex)), doc,SLOT(saveSpaceAsDictionary(QModelIndex)));
0136 
0137     m_document = doc;
0138 
0139     m_spacesProxyModel->setSourceModel(doc->spacesModel());
0140 
0141     m_ui->spacesView->setModel(m_spacesProxyModel);
0142     
0143     m_document->currentPlots()->setFilterSpaceDimension(Dim2D);
0144     m_ui->plotsView2D->setModel(m_document->currentPlots());
0145     m_ui->plotsView2D->setSelectionModel(m_document->currentSelectionModel());
0146 
0147     m_document->currentPlots()->setFilterSpaceDimension(Dim3D);
0148     m_ui->plotsView3D->setModel(m_document->currentPlots());
0149     m_ui->plotsView3D->setSelectionModel(m_document->currentSelectionModel());
0150     m_ui->plotsView3D->setUseSimpleRotation(true);
0151 
0152     connect(m_document->currentPlots(), SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(setCurrentPlot(QModelIndex,int)));
0153     connect(m_document->spacesModel(), SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(setCurrentSpace(QModelIndex,int)));
0154 }
0155 
0156 QPixmap Dashboard::thumbnail(Analitza::Dimension dim) const
0157 {
0158     QPixmap thumbnail;
0159     switch (dim)
0160     {
0161     case Analitza::Dim2D:
0162         thumbnail = m_ui->plotsView2D->grab();
0163         break;
0164     case Analitza::Dim3D: {
0165         m_ui->plotsView3D->update();
0166         thumbnail = QPixmap::fromImage(m_ui->plotsView3D->grabFramebuffer(), Qt::ColorOnly);
0167         break;
0168     }
0169     }
0170     return thumbnail;
0171 }
0172 
0173 PlotsView2D* Dashboard::view2d()
0174 {
0175     return m_ui->plotsView2D;
0176 }
0177 
0178 PlotsView3DES* Dashboard::view3d()
0179 {
0180     return m_ui->plotsView3D;
0181 }
0182 
0183 void Dashboard::goHome()
0184 {
0185     setCurrentIndex(0);
0186     m_ui->views->setCurrentIndex(0);
0187     emit showFilter(true);
0188 }
0189 
0190 void Dashboard::showDictionary()
0191 {
0192     m_ui->views->setCurrentIndex(1);
0193     emit showFilter(false);
0194 }
0195 
0196 void Dashboard::setDictionaryData(int ind)
0197 {
0198     PlotsDictionaryModel *model = m_document->plotsDictionaryModel();
0199     m_plotdictionarymodel=model;
0200     model->clear();
0201     QString fileName=m_ui->comboBox->itemText(ind).append(".plots");
0202     QString dirPath=m_DictionaryPathName.key(fileName);
0203     dirPath.append(fileName);
0204     model->createDictionary(dirPath);
0205     m_ui->plotsView->setModel(model);
0206     m_ui->page->setModel(model->plotModel());
0207     m_ui->page_2->setModel(model->plotModel());
0208     connect(m_ui->plotsView->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(setModelIndex(QModelIndex)));
0209 }
0210 
0211 void Dashboard::setModelIndex(const QModelIndex& ind)
0212 {
0213     m_plotdictionarymodel->setCurrentIndex(ind);
0214     setPlotsView(m_plotdictionarymodel->dimension());
0215 }
0216 
0217 void Dashboard::setPlotsView(Analitza::Dimension dim)
0218 {
0219     if(dim==Dim2D){
0220         m_ui->stackedWidget->setCurrentIndex(0);
0221 
0222     } else if (dim==Dim3D){
0223         m_ui->stackedWidget->setCurrentIndex(1);
0224     }
0225 }
0226 
0227 void Dashboard::importDictionaryClicked()
0228 {
0229     const QString path = QFileDialog::getOpenFileName(this, i18n( "Open" ), {},
0230                      i18n( "Dictionary Files (*.plots);;All Files (*)" ) );
0231 
0232     if(!path.isEmpty()) {
0233         int currentIndex=m_ui->comboBox->count();
0234         m_ui->comboBox->addItem(QFileInfo(path).baseName());
0235         m_ui->comboBox->setCurrentIndex(currentIndex);
0236         m_DictionaryPathName.insertMulti(QFileInfo(path).path().append("/"),QFileInfo(path).fileName());
0237         setDictionaryData(currentIndex);
0238     }
0239 }
0240 
0241 void Dashboard::showPlotsView2D()
0242 {
0243     m_ui->plotsViews->setCurrentIndex(0);
0244 }
0245 
0246 void Dashboard::showPlotsView3D()
0247 {
0248     m_ui->plotsViews->setCurrentIndex(1);
0249 }
0250 
0251 void Dashboard::exportSpaceSnapshot(Dimension dim)
0252 {
0253     QString path = QFileDialog::getSaveFileName(this, i18n("Where to export"), {}, i18n("PNG Image File (*.png)"));
0254     if(!path.isEmpty()){
0255         if(dim==Dim2D){
0256             view2d()->toImage(path,PlotsView2D::PNG);
0257         } else if(dim==Dim3D){
0258             view3d()->save(QUrl::fromLocalFile(path));
0259         }
0260     }
0261 }
0262 
0263 void Dashboard::copySpace2DSnapshotToClipboard()
0264 {
0265     view2d()->snapshotToClipboard();
0266 }
0267 
0268 void Dashboard::copySpace3DSnapshotToClipboard()
0269 {
0270     QClipboard *cb = QApplication::clipboard();
0271     cb->setImage(view3d()->grabFramebuffer());
0272 }
0273 
0274 void Dashboard::filterByText(const QString &text)
0275 {
0276     m_spacesProxyModel->setFilterText(text);
0277 }
0278 
0279 void Dashboard::filterByDimension(Dimensions dim)
0280 {
0281     static_cast<SpacesDelegate*>(m_ui->spacesView->itemDelegate())->filterEvent();
0282     m_spacesProxyModel->setFilterDimension(dim);
0283 }
0284 
0285 void Dashboard::setCurrentSpace(const QModelIndex &index)
0286 {
0287     setCurrentIndex(1);
0288 
0289     QModelIndex ind;
0290     if(index.model()==m_spacesProxyModel)
0291         ind=m_spacesProxyModel->mapToSource(index);
0292     else
0293         ind=index;
0294 
0295     emit spaceActivated(ind.row());
0296 
0297     switch (m_document->spacesModel()->space(ind.row())->dimension())
0298     {
0299     case Dim2D:
0300         m_ui->plotsViews->setCurrentIndex(0);
0301         //set dialog visible
0302         emit restoreDictionaryData(Dim2D);
0303         break;
0304 
0305     case Dim3D:
0306         m_ui->plotsViews->setCurrentIndex(1);
0307         //set dialog invisible
0308         emit restoreDictionaryData(Dim3D);
0309         break;
0310 
0311     default: break;
0312     }
0313 
0314     // identification of the space
0315     if(!(m_spaceindexList.contains(ind)) && m_openclicked){
0316         m_spaceindexList.append(ind);
0317         emit plotRequested(ind);
0318     }
0319 
0320  emit showFilter(false);
0321 }
0322 
0323 //FIXME: 3 methods called setCurrentSpace is too much
0324 void Dashboard::setCurrentSpace(const QModelIndex& index, const QModelIndex& old)
0325 {
0326     Q_UNUSED(old);
0327     m_document->setCurrentSpace(index.row());
0328 }
0329 
0330 void Dashboard::setCurrentSpace(const QModelIndex& index, int row)
0331 {
0332     Q_UNUSED(index);
0333 //     qDebug() << "sadasadadsd ads";
0334 //     m_widget->spacesView->selectionModel()->setCurrentIndex(m_document->spacesModel()->index(row), QItemSelectionModel::Current);
0335 
0336     m_ui->spacesView->setCurrentIndex(m_document->spacesModel()->index(row));
0337 
0338 //     m_document->currentPlots()->setFilterSpaceDimension(m_document->spacesModel()->item(row)->dimension());
0339 //     m_document->currentPlots()->setFilterSpace(m_document->spacesModel()->item(row));
0340 
0341     emit spaceActivated(row);
0342     
0343     //BUG qt el filtermodel no respeta iconviewmode al agregar los items ...
0344 //     if (m_document->spacesModel()->rowCount() < 2) // si esta vacio por primera actiualizo el view 
0345 //         m_widget->spacesView->update();
0346     
0347     static_cast<SpacesDelegate*>(m_ui->spacesView->itemDelegate())->filterEvent();
0348 }
0349 
0350 void Dashboard::setCurrentPlot(const QModelIndex& parent, int start)
0351 {
0352     Q_ASSERT(!parent.isValid());
0353     m_document->currentSelectionModel()->clear();
0354     m_document->currentSelectionModel()->setCurrentIndex(m_document->currentPlots()->index(start,0), QItemSelectionModel::SelectCurrent );
0355 }
0356 
0357 void Dashboard::setGridColor(const QColor &color) // used for making the axis visible/invisible
0358 {
0359     if (m_document->spacesModel()->space(m_document->currentSpace())->dimension()==Dim2D) // 2d space
0360         m_ui->plotsView2D->setGridColor(color);
0361     else 
0362         if (m_document->spacesModel()->space(m_document->currentSpace())->dimension()==Dim3D) // 3d space
0363         { 
0364             auto plotter3d = dynamic_cast<Analitza::Plotter3DES*>(view3d());
0365             plotter3d->setReferencePlaneColor(color);
0366         }
0367 }
0368 
0369 void Dashboard::setGridStyle(int i)
0370 {
0371     if (m_document->spacesModel()->space(m_document->currentSpace())->dimension()==Dim2D) // 2d space
0372     {
0373         switch (i)
0374         {
0375             case 0: 
0376                 m_ui->plotsView2D->setShowGrid(false);
0377                 break;
0378             case 1: 
0379                 m_ui->plotsView2D->setShowGrid(true);
0380                 m_ui->plotsView2D->setGridStyleHint(Analitza::Squares);
0381                 break;
0382             case 2: 
0383                 m_ui->plotsView2D->setShowGrid(true);
0384                 m_ui->plotsView2D->setGridStyleHint(Analitza::Circles);
0385                 break;
0386         }
0387     }
0388     else 
0389         if (m_document->spacesModel()->space(m_document->currentSpace())->dimension()==Dim3D) // 3d space
0390         {
0391             switch (i)
0392             {
0393                 case 0: 
0394                     m_ui->plotsView2D->setShowGrid(false);
0395                     break;
0396                 case 1: 
0397                     m_ui->plotsView2D->setShowGrid(true);
0398                     break;
0399                 case 2: 
0400                     m_ui->plotsView2D->setShowGrid(true);
0401                     break;
0402             }
0403         }
0404 }
0405 
0406 void Dashboard::setPlotsViewGridColor(const QColor &color)
0407 {
0408     if(m_document->spacesModel()->space(m_document->currentSpace())->dimension()==Dim2D){ // 2d space
0409         m_ui->plotsView2D->setGridColor(color);
0410     } else if(m_document->spacesModel()->space(m_document->currentSpace())->dimension()==Dim3D){ // 3d space
0411         auto plotter3d = dynamic_cast<Analitza::Plotter3DES*>(view3d());
0412         plotter3d->setReferencePlaneColor(color);
0413         showPlotsView3D();
0414     }
0415 }
0416 
0417 void Dashboard::getDictionaryClicked()
0418 {
0419     KNS3::DownloadDialog newStuffDialog(this);
0420     newStuffDialog.exec();
0421     KNS3::Entry::List installedentries = newStuffDialog.installedEntries();
0422 }
0423 
0424 void Dashboard::activateDictionaryData() {
0425     setDictionaryData(m_ui->comboBox->currentIndex());
0426 }
0427 
0428 #include "moc_dashboard.cpp"