1 /**********************************************************************************************
2     Copyright (C) 2017 Norbert Truchsess <norbert.truchsess@t-online.de>
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 
18 **********************************************************************************************/
19 
20 #include "helpers/CToolBarConfig.h"
21 #include "helpers/CToolBarSetupDialog.h"
22 
23 #include <QAction>
24 
shouldBeMoved(QListWidgetItem * item)25 bool CToolBarSetupDialog::CItemFilter::shouldBeMoved(QListWidgetItem* item)
26 {
27     CDialogItem* dialogItem = dynamic_cast<CDialogItem*>(item);
28     if (dialogItem != nullptr)
29     {
30         return dialogItem->actionName != "separator";
31     }
32     return true;
33 }
34 
CToolBarSetupDialog(QWidget * const & parent,CToolBarConfig * const & config)35 CToolBarSetupDialog::CToolBarSetupDialog(QWidget* const& parent, CToolBarConfig* const& config) : QDialog(parent), config(config)
36 {
37     setupUi(this);
38 
39     selectActionsWidget->setFilter(new CItemFilter(this));
40 
41     connect(buttonBox, &QDialogButtonBox::clicked, this, &CToolBarSetupDialog::slotButtonClicked);
42 
43     configure();
44 
45     selectActionsWidget->setLabelAvailable(tr("Available Actions"));
46     selectActionsWidget->setLabelSelected(tr("Selected Actions"));
47 }
48 
~CToolBarSetupDialog()49 CToolBarSetupDialog::~CToolBarSetupDialog()
50 {
51     selectActionsWidget->clear();
52 }
53 
accept()54 void CToolBarSetupDialog::accept()
55 {
56     QStringList actionNames;
57     for (const QListWidgetItem* const selectedItem : selectActionsWidget->selected())
58     {
59         const CDialogItem* const setupDialogItem = dynamic_cast<const CDialogItem* const>(selectedItem);
60         if (setupDialogItem != nullptr)
61         {
62             actionNames << setupDialogItem->actionName;
63         }
64     }
65     config->setConfiguredActionsByName(actionNames);
66     config->setVisibleInFullscreen(checkFullscreen->isChecked());
67     QDialog::accept();
68 }
69 
slotButtonClicked(QAbstractButton * button) const70 void CToolBarSetupDialog::slotButtonClicked(QAbstractButton* button) const
71 {
72     if(buttonBox->buttonRole(button) == QDialogButtonBox::ResetRole)
73     {
74         config->setDefaultConfiguredActions();
75         configure();
76     }
77 }
78 
configure() const79 void CToolBarSetupDialog::configure() const
80 {
81     QList<QListWidgetItem*> availableItems;
82     QList<QListWidgetItem*> selectedItems;
83 
84     for(QAction* const& action : config->availableActions())
85     {
86         availableItems << new CDialogItem(action->icon(), action->iconText(), action->objectName());
87     }
88     for(QAction* const& action : config->configuredActions())
89     {
90         if (action->isSeparator())
91         {
92             selectedItems << new CDialogItem(action->icon(), "---------------", action->objectName());
93         }
94         else
95         {
96             QString configuredName = action->objectName();
97             for(QListWidgetItem* const& item : qAsConst(availableItems))
98             {
99                 if(configuredName == dynamic_cast<CDialogItem* const>(item)->actionName)
100                 {
101                     selectedItems << item;
102                     break;
103                 }
104             }
105         }
106     }
107     selectActionsWidget->setSelected(selectedItems);
108     selectActionsWidget->setAvailable(availableItems);
109     selectActionsWidget->sortAvailable();
110     checkFullscreen->setChecked(config->visibleInFullscreen());
111 }
112 
113