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 "tabdialog.h"
21 #include "ui_tabdialog.h"
22 
23 #include <QPushButton>
24 
TabDialog(TabDialog::TabDialogType type,QWidget * parent)25 TabDialog::TabDialog(TabDialog::TabDialogType type, QWidget *parent)
26     : QDialog(parent)
27     , ui(new Ui::TabDialog)
28     , m_tabGroupName()
29     , m_tabs()
30 {
31     ui->setupUi(this);
32 
33     if (type == TabNew) {
34         setWindowTitle( tr("New Tab") );
35         setWindowIcon( QIcon(":/images/tab_new") );
36     } else if (type == TabRename) {
37         setWindowTitle( tr("Rename Tab") );
38         setWindowIcon( QIcon(":/images/tab_rename") );
39     } else {
40         setWindowTitle( tr("Rename Tab Group") );
41         setWindowIcon( QIcon(":/images/tab_rename") );
42     }
43 
44     connect( this, &TabDialog::accepted,
45              this, &TabDialog::onAccepted );
46 
47     connect( ui->lineEditTabName, &QLineEdit::textChanged,
48              this, &TabDialog::validate );
49 
50     validate();
51 }
52 
~TabDialog()53 TabDialog::~TabDialog()
54 {
55     delete ui;
56 }
57 
setTabIndex(int tabIndex)58 void TabDialog::setTabIndex(int tabIndex)
59 {
60     m_tabIndex = tabIndex;
61 }
62 
setTabs(const QStringList & tabs)63 void TabDialog::setTabs(const QStringList &tabs)
64 {
65     m_tabs = tabs;
66     validate();
67 }
68 
setTabName(const QString & tabName)69 void TabDialog::setTabName(const QString &tabName)
70 {
71     ui->lineEditTabName->setText(tabName);
72     ui->lineEditTabName->selectAll();
73 }
74 
setTabGroupName(const QString & tabGroupName)75 void TabDialog::setTabGroupName(const QString &tabGroupName)
76 {
77     m_tabGroupName = tabGroupName;
78     ui->lineEditTabName->setText(m_tabGroupName);
79 }
80 
validate()81 void TabDialog::validate()
82 {
83     bool ok = true;
84     const QString text = ui->lineEditTabName->text();
85 
86     if ( m_tabGroupName.isEmpty() ) {
87         ok = !text.isEmpty() && !m_tabs.contains(text);
88     } else {
89         const QString tabPrefix = m_tabGroupName + '/';
90         for (const auto &tab : m_tabs) {
91             if ( tab == m_tabGroupName || tab.startsWith(tabPrefix) ) {
92                 const QString newName = text + tab.mid(m_tabGroupName.size());
93                 if ( newName.isEmpty() || m_tabs.contains(newName) ) {
94                     ok = false;
95                     break;
96                 }
97             }
98         }
99     }
100 
101     ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
102 }
103 
onAccepted()104 void TabDialog::onAccepted()
105 {
106     const auto newName = ui->lineEditTabName->text();
107 
108     if ( m_tabGroupName.isEmpty() && m_tabIndex == -1 )
109         emit newTabNameAccepted(newName);
110     else if ( m_tabGroupName.isEmpty() )
111         emit barTabNameAccepted(newName, m_tabIndex);
112     else
113         emit treeTabNameAccepted(newName, m_tabGroupName);
114 }
115