1 /*
2     Copyright (c) 2020, Lukas Holecek <hluk@email.cz>
3 
4     This file is part of CopyQ.
5 
6     CopyQ is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10 
11     CopyQ is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with CopyQ.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "gui/importexportdialog.h"
21 #include "ui_importexportdialog.h"
22 
23 #include "gui/tabicons.h"
24 
25 #include <QPushButton>
26 
ImportExportDialog(QWidget * parent)27 ImportExportDialog::ImportExportDialog(QWidget *parent)
28     : QDialog(parent)
29     , ui(new Ui::ImportExportDialog)
30 {
31     ui->setupUi(this);
32 
33     connect(ui->checkBoxAll, &QCheckBox::clicked,
34             this, &ImportExportDialog::onCheckBoxAllClicked);
35 
36     connect( ui->listTabs, &QListWidget::itemSelectionChanged,
37              this, &ImportExportDialog::update );
38     connect( ui->checkBoxConfiguration, &QCheckBox::stateChanged,
39              this, &ImportExportDialog::update );
40     connect( ui->checkBoxCommands, &QCheckBox::stateChanged,
41              this, &ImportExportDialog::update );
42 }
43 
~ImportExportDialog()44 ImportExportDialog::~ImportExportDialog()
45 {
46     delete ui;
47 }
48 
setTabs(const QStringList & tabs)49 void ImportExportDialog::setTabs(const QStringList &tabs)
50 {
51     ui->listTabs->clear();
52     ui->listTabs->addItems(tabs);
53     ui->listTabs->selectAll();
54     const auto items = ui->listTabs->selectedItems();
55     for (const auto item : items)
56         item->setIcon( getIconForTabName(item->text()) );
57 
58     const bool showTabs = ui->listTabs->count() > 0;
59     ui->listTabs->setVisible(showTabs);
60     ui->labelTabs->setVisible(showTabs);
61 }
62 
setCurrentTab(const QString & tabName)63 void ImportExportDialog::setCurrentTab(const QString &tabName)
64 {
65     const auto items = ui->listTabs->findItems(tabName, Qt::MatchExactly);
66     if ( !items.isEmpty() )
67         ui->listTabs->setCurrentItem( items.first() );
68 }
69 
setHasConfiguration(bool hasConfiguration)70 void ImportExportDialog::setHasConfiguration(bool hasConfiguration)
71 {
72     ui->checkBoxConfiguration->setVisible(hasConfiguration);
73 }
74 
setHasCommands(bool hasCommands)75 void ImportExportDialog::setHasCommands(bool hasCommands)
76 {
77     ui->checkBoxCommands->setVisible(hasCommands);
78 }
79 
setConfigurationEnabled(bool enabled)80 void ImportExportDialog::setConfigurationEnabled(bool enabled)
81 {
82     ui->checkBoxConfiguration->setChecked(enabled);
83 }
84 
setCommandsEnabled(bool enabled)85 void ImportExportDialog::setCommandsEnabled(bool enabled)
86 {
87     ui->checkBoxCommands->setChecked(enabled);
88 }
89 
selectedTabs() const90 QStringList ImportExportDialog::selectedTabs() const
91 {
92     const auto items = ui->listTabs->selectedItems();
93     QStringList result;
94     result.reserve( items.size() );
95     for (const auto item : items)
96         result.append( item->text() );
97     return result;
98 }
99 
isConfigurationEnabled() const100 bool ImportExportDialog::isConfigurationEnabled() const
101 {
102     return ui->checkBoxConfiguration->isChecked()
103             && !ui->checkBoxConfiguration->isHidden();
104 }
105 
isCommandsEnabled() const106 bool ImportExportDialog::isCommandsEnabled() const
107 {
108     return ui->checkBoxCommands->isChecked()
109             && !ui->checkBoxCommands->isHidden();
110 }
111 
onCheckBoxAllClicked(bool checked)112 void ImportExportDialog::onCheckBoxAllClicked(bool checked)
113 {
114     ui->checkBoxConfiguration->setChecked(checked);
115     ui->checkBoxCommands->setChecked(checked);
116 
117     if (checked)
118         ui->listTabs->selectAll();
119     else
120         ui->listTabs->clearSelection();
121 }
122 
update()123 void ImportExportDialog::update()
124 {
125     const bool ok = canAccept();
126 
127     auto button = ui->buttonBox->button(QDialogButtonBox::Ok);
128     if (button)
129         button->setEnabled(ok);
130 
131     if (!ok) {
132         ui->checkBoxAll->setCheckState(Qt::Unchecked);
133     } else if ( ui->listTabs->selectedItems().count() == ui->listTabs->count()
134                 && (ui->checkBoxConfiguration->isChecked() || ui->checkBoxConfiguration->isHidden())
135                 && (ui->checkBoxCommands->isChecked() || ui->checkBoxCommands->isHidden()) )
136     {
137         ui->checkBoxAll->setCheckState(Qt::Checked);
138     } else {
139         ui->checkBoxAll->setCheckState(Qt::PartiallyChecked);
140     }
141 }
142 
canAccept() const143 bool ImportExportDialog::canAccept() const
144 {
145     return ui->listTabs->selectionModel()->hasSelection()
146             || isConfigurationEnabled()
147             || isCommandsEnabled();
148 }
149