File indexing completed on 2024-04-21 05:30:55

0001 /*
0002     SPDX-FileCopyrightText: 2021 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "exporttemplatedialog.h"
0007 
0008 // local
0009 #include "ui_exporttemplatedialog.h"
0010 #include "exporttemplatehandler.h"
0011 #include "../settingsdialog/layoutscontroller.h"
0012 #include "../../layouts/manager.h"
0013 #include "../../layouts/synchronizer.h"
0014 #include "../../settings/universalsettings.h"
0015 #include "../../view/view.h"
0016 
0017 namespace Latte {
0018 namespace Settings {
0019 namespace Dialog {
0020 
0021 ExportTemplateDialog::ExportTemplateDialog(QDialog *parent)
0022     : GenericDialog(parent),
0023       m_ui(new Ui::ExportTemplateDialog)
0024 {
0025     setAttribute(Qt::WA_DeleteOnClose, true);
0026 }
0027 
0028 ExportTemplateDialog::ExportTemplateDialog(SettingsDialog *parent, const Data::Layout &layout)
0029     : ExportTemplateDialog(parent)
0030 {
0031     setAttribute(Qt::WA_DeleteOnClose, true);
0032     m_corona = parent->corona();
0033 
0034     init();
0035     initExportButton(i18n("Export your selected layout as template"));
0036     //! we must create handlers after creating/adjusting the ui
0037     m_handler = new Handler::ExportTemplateHandler(this, layout);
0038     initSignals();
0039 }
0040 
0041 ExportTemplateDialog::ExportTemplateDialog(ViewsDialog *parent, const Data::View &view)
0042     : ExportTemplateDialog(parent)
0043 {
0044     setAttribute(Qt::WA_DeleteOnClose, true);
0045     m_corona = parent->corona();
0046 
0047     init();
0048     initExportButton(i18n("Export your selected dock or panel as template"));
0049     //! we must create handlers after creating/adjusting the ui
0050     m_handler = new Handler::ExportTemplateHandler(this, view);
0051     initSignals();
0052 }
0053 
0054 ExportTemplateDialog::ExportTemplateDialog(Latte::View *view)
0055     : GenericDialog(nullptr),
0056       m_ui(new Ui::ExportTemplateDialog)/*this is necessary, in order to create the ui*/
0057 {
0058     setAttribute(Qt::WA_DeleteOnClose, true);
0059     m_corona = qobject_cast<Latte::Corona *>(view->corona());
0060 
0061     init();
0062     initExportButton(i18n("Export your selected view as template"));
0063     //! we must create handlers after creating/adjusting the ui
0064     m_handler = new Handler::ExportTemplateHandler(this, view);
0065     initSignals();
0066 }
0067 
0068 ExportTemplateDialog::~ExportTemplateDialog()
0069 {
0070 }
0071 
0072 Latte::Corona *ExportTemplateDialog::corona() const
0073 {
0074     return m_corona;
0075 }
0076 
0077 Ui::ExportTemplateDialog *ExportTemplateDialog::ui() const
0078 {
0079     return m_ui;
0080 }
0081 
0082 void ExportTemplateDialog::init()
0083 {
0084     setAttribute(Qt::WA_DeleteOnClose, true);
0085     //! first we need to setup the ui
0086     m_ui->setupUi(this);
0087     initButtons();
0088 
0089     //! Update ALL active original layouts before exporting,
0090     m_corona->layoutsManager()->synchronizer()->syncActiveLayoutsToOriginalFiles();
0091     m_corona->universalSettings()->syncSettings();
0092 }
0093 
0094 void ExportTemplateDialog::initButtons()
0095 {
0096     m_ui->buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
0097     connect(m_ui->buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked,
0098             this, &ExportTemplateDialog::onCancel);
0099 }
0100 
0101 void ExportTemplateDialog::initExportButton(const QString &tooltip)
0102 {
0103     m_exportButton = new QPushButton(this);
0104     m_exportButton->setText(i18nc("export template", "Export"));
0105     m_exportButton->setIcon(QIcon::fromTheme("document-export"));
0106     m_exportButton->setToolTip(tooltip);
0107 
0108     m_ui->buttonBox->addButton(m_exportButton, QDialogButtonBox::AcceptRole);
0109 }
0110 
0111 void ExportTemplateDialog::initSignals()
0112 {
0113     connect(m_handler, &Handler::ExportTemplateHandler::dataChanged, this, &ExportTemplateDialog::onDataChanged);
0114     connect(m_handler, &Handler::ExportTemplateHandler::exportSucceeded, this, &ExportTemplateDialog::onExportSucceeded);
0115 }
0116 
0117 QPushButton *ExportTemplateDialog::exportButton() const
0118 {
0119     return m_exportButton;
0120 }
0121 
0122 void ExportTemplateDialog::onDataChanged()
0123 {
0124     m_ui->buttonBox->button(QDialogButtonBox::Reset)->setEnabled(!m_handler->inDefaultValues());
0125 }
0126 
0127 void ExportTemplateDialog::onExportSucceeded()
0128 {
0129     m_exportButton->setText(i18nc("export template", "Export Again"));
0130 }
0131 
0132 void ExportTemplateDialog::accept()
0133 {
0134     qDebug() << Q_FUNC_INFO;
0135     //close();
0136 }
0137 
0138 void ExportTemplateDialog::onCancel()
0139 {
0140     qDebug() << Q_FUNC_INFO;
0141     close();
0142 }
0143 
0144 void ExportTemplateDialog::onReset()
0145 {
0146     qDebug() << Q_FUNC_INFO;
0147     close();
0148 }
0149 
0150 }
0151 }
0152 }