File indexing completed on 2024-05-12 07:41:27

0001 /*
0002     File                 : TemplateChooserDialog.cpp
0003     Project              : LabPlot
0004     Description          : dialog to load user-defined plot definitions
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2022 Martin Marmsoler <martin.marmsoler@gmail.com>
0007     SPDX-FileCopyrightText: 2022 Alexander Semke <alexander.semke@web.de>
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #ifndef PLOTTEMPLATEDIALOG_H
0012 #define PLOTTEMPLATEDIALOG_H
0013 
0014 #include <QAbstractListModel>
0015 #include <QDialog>
0016 
0017 class Project;
0018 class Worksheet;
0019 class TemplateListModel;
0020 class CartesianPlot;
0021 
0022 namespace Ui {
0023 class PlotTemplateDialog;
0024 }
0025 
0026 class PlotTemplateDialog : public QDialog {
0027     Q_OBJECT
0028 
0029 public:
0030     explicit PlotTemplateDialog(QWidget* parent = nullptr);
0031     void updateErrorMessage(const QString&);
0032     ~PlotTemplateDialog();
0033 
0034     QString templatePath() const;
0035     void showPreview();
0036     static const QString format;
0037     static QString defaultTemplateInstallPath();
0038     CartesianPlot* generatePlot();
0039 
0040 private:
0041     void chooseTemplateSearchPath();
0042     void listViewTemplateChanged(const QModelIndex& current, const QModelIndex& previous);
0043     void changePreviewSource(int row);
0044     void customTemplatePathChanged(const QString&);
0045 
0046 private:
0047     Ui::PlotTemplateDialog* ui;
0048     Project* m_project; // TODO: use smart pointer!
0049     Worksheet* m_worksheet;
0050     QWidget* m_worksheetView;
0051     TemplateListModel* mTemplateListModelDefault;
0052     TemplateListModel* mTemplateListModelCustom;
0053     bool mLoading{false};
0054 };
0055 
0056 class TemplateListModel : public QAbstractListModel {
0057 public:
0058     explicit TemplateListModel(const QString& searchPath, QObject* parent = nullptr);
0059     void setSearchPath(const QString& searchPath);
0060     virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
0061     virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
0062     QString searchPath() {
0063         return mSearchPath;
0064     }
0065 
0066     enum Roles {
0067         FilenameRole = Qt::ItemDataRole::UserRole + 1,
0068         FilePathRole,
0069     };
0070 
0071     struct File {
0072         QString path; // path with filename name and extension. Used to create plot
0073         QString filename; // filename only without extension. Visible part in the view
0074     };
0075 
0076 private:
0077     QVector<File> mFiles;
0078     QString mSearchPath;
0079 };
0080 
0081 #endif // PLOTTEMPLATEDIALOG_H