1 /*
2 **********************************************************************************
3 **
4 ** This file was created for the LibreCAD project (librecad.org), a 2D CAD program.
5 **
6 ** Copyright (C) 2015 ravas (ravas@outlook.com)
7 **
8 ** This program is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License
10 ** as published by the Free Software Foundation; either version 2
11 ** of the License, or (at your option) any later version.
12 **
13 ** This program is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with this program; if not, write to the Free Software
20 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 **
22 **********************************************************************************
23 */
24 
25 // This file was first published at: github.com/r-a-v-a-s/LibreCAD.git
26 
27 #include "lc_customtoolbar.h"
28 
29 #include <QTextStream>
30 #include <QFile>
31 #include <QAction>
32 
LC_CustomToolbar(QWidget * parent)33 LC_CustomToolbar::LC_CustomToolbar(QWidget* parent)
34     : QToolBar(parent)
35 {
36     QAction* action = new QAction(tr("Add or Remove Action"), parent);
37     action->setShortcut(QKeySequence("F2"));
38     action->setIcon(QIcon(":/extui/char_pm.png"));
39     connect(action, SIGNAL(triggered()), this, SLOT(slot_add_or_remove_action()));
40     addAction(action);
41 }
42 
~LC_CustomToolbar()43 LC_CustomToolbar::~LC_CustomToolbar()
44 {
45     if (!file_path.isNull())
46     {
47         QFile file(file_path);
48         if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
49             return;
50 
51         QTextStream txt_stream(&file);
52         foreach (const QString& token, state_list)
53         {
54             txt_stream << token << "\n";
55         }
56     }
57 }
58 
actions_from_file(const QString & path,QMap<QString,QAction * > & a_map)59 void LC_CustomToolbar::actions_from_file(const QString& path,
60                                          QMap<QString, QAction*>& a_map)
61 {
62     QFile file(path);
63     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
64         return;
65 
66     file_path = path;
67 
68     QTextStream txt_stream(&file);
69     QString line;
70     while (!txt_stream.atEnd())
71     {
72         line = txt_stream.readLine();
73 
74         if (line == "-")
75         {
76             addSeparator();
77             state_list << line;
78         }
79         else if (a_map.contains(line))
80         {
81             addAction(a_map[line]);
82             state_list << line;
83         }
84     }
85 }
86 
slot_add_or_remove_action()87 void LC_CustomToolbar::slot_add_or_remove_action()
88 {
89     if (most_recent_action )
90     {
91         QString token = most_recent_action->objectName();
92 
93         if (state_list.contains(token))
94         {
95             removeAction(most_recent_action);
96             state_list.removeOne(token);
97         }
98         else
99         {
100             addAction(most_recent_action);
101             state_list << token;
102         }
103     }
104 }
105 
slot_most_recent_action(QAction * q_action)106 void LC_CustomToolbar::slot_most_recent_action(QAction* q_action)
107 {
108     most_recent_action = q_action;
109 }
110 
111 
add_separator()112 void LC_CustomToolbar::add_separator()
113 {
114     addSeparator();
115     state_list << "-";
116 }
117