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 "configtabtabs.h"
21
22 #include "common/appconfig.h"
23 #include "common/tabs.h"
24 #include "gui/iconfactory.h"
25 #include "gui/itemorderlist.h"
26 #include "gui/tabicons.h"
27 #include "gui/tabpropertieswidget.h"
28
29 #include <QSettings>
30 #include <QVBoxLayout>
31
32 Q_DECLARE_METATYPE(TabProperties)
33
34 namespace {
35
36 class TabItem final : public ItemOrderList::Item {
37 public:
TabItem(const TabProperties & tab,ItemOrderList * parentList)38 TabItem(const TabProperties &tab, ItemOrderList *parentList)
39 : m_tabProperties(tab)
40 , m_parentList(parentList)
41 {
42 }
43
data() const44 QVariant data() const override
45 {
46 return QVariant::fromValue(m_tabProperties);
47 }
48
49 private:
createWidget(QWidget * parent)50 QWidget *createWidget(QWidget *parent) override
51 {
52 auto widget = new TabPropertiesWidget(parent);
53
54 widget->setTabName(m_tabProperties.name);
55 widget->setIconName(m_tabProperties.iconName);
56 widget->setMaxItemCount(m_tabProperties.maxItemCount);
57 widget->setStoreItems(m_tabProperties.storeItems);
58
59 QObject::connect( widget, &TabPropertiesWidget::iconNameChanged,
60 [&](const QString &icon) { m_tabProperties.iconName = icon; } );
61 QObject::connect( widget, &TabPropertiesWidget::maxItemCountChanged,
62 [&](int count) { m_tabProperties.maxItemCount = count; } );
63 QObject::connect( widget, &TabPropertiesWidget::storeItemsChanged,
64 [&](bool store) { m_tabProperties.storeItems = store; } );
65
66 QObject::connect(
67 widget, &TabPropertiesWidget::iconNameChanged,
68 m_parentList, [&] (const QString &iconName) {
69 const auto row = m_parentList->currentRow();
70 const auto icon = iconName.isEmpty() ? QIcon() : iconFromFile(iconName);
71 m_parentList->setItemIcon(row, icon);
72 }
73 );
74
75 return widget;
76 }
77
78 TabProperties m_tabProperties;
79 ItemOrderList *m_parentList;
80 };
81
82 } // namespace
83
ConfigTabTabs(QWidget * parent)84 ConfigTabTabs::ConfigTabTabs(QWidget *parent)
85 : QWidget(parent)
86 , m_list(new ItemOrderList(this))
87 {
88 m_list->setItemsMovable(true);
89
90 auto layout = new QVBoxLayout(this);
91 layout->setContentsMargins(0, 0, 0, 0);
92 layout->addWidget(m_list);
93
94 const Tabs tabs;
95 for (const auto &name : AppConfig().option<Config::tabs>()) {
96 const auto icon = getIconForTabName(name);
97 ItemOrderList::ItemPtr item(new TabItem(tabs.tabProperties(name), m_list));
98 m_list->appendItem(name, icon, item);
99 }
100 }
101
saveTabs(QSettings * settings)102 void ConfigTabTabs::saveTabs(QSettings *settings)
103 {
104 Tabs tabs;
105 QStringList tabList;
106 for (int row = 0; row < m_list->itemCount(); ++row) {
107 const auto name = m_list->itemLabel(row);
108 if (name.isEmpty())
109 continue;
110
111 tabList.append(name);
112 tabs.setTabProperties( m_list->data(row).value<TabProperties>() );
113 }
114
115 tabs.save(settings, tabList);
116
117 AppConfig().setOption("tabs", tabList);
118 }
119