GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ToolBarButton.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2011-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 // This file should not include config.h. It is only included in other
27 // C++ source files that should have included config.h before including
28 // this file.
29 
30 #include <QAction>
31 #include <QIcon>
32 #include <QString>
33 #include <QWidget>
34 
35 #include "ToolBarButton.h"
36 #include "QtHandlesUtils.h"
37 #include "octave-qobject.h"
38 #include "gui-preferences-global.h"
39 
41 
42 template <typename T>
43 ToolBarButton<T>::ToolBarButton (octave::base_qobject& oct_qobj,
44  octave::interpreter& interp,
45  const graphics_object& go, QAction *action)
46 : Object (oct_qobj, interp, go, action), m_separator (nullptr)
47 {
48  typename T::properties& tp = properties<T> ();
49 
50  action->setToolTip (Utils::fromStdString (tp.get_tooltipstring ()));
51  action->setVisible (tp.is_visible ());
52 
53  // Get the icon data from cdata or as a named icon
54  QImage img = Utils::makeImageFromCData (tp.get_cdata (), 24, 24);
55 
56  if (img.width () == 0)
57  {
58  QIcon ico;
59  std::string name = tp.get___named_icon__ ();
60  if (! name.empty ())
61  ico = get_icon (name);
62 
63  action->setIcon (ico);
64  }
65  else
66  action->setIcon (QIcon (QPixmap::fromImage (img)));
67 
68  if (tp.is_separator ())
69  {
70  m_separator = new QAction (action);
71  m_separator->setSeparator (true);
72  m_separator->setVisible (tp.is_visible ());
73  }
74  action->setEnabled (tp.is_enable ());
75 
76  QWidget *w = qobject_cast<QWidget *> (action->parent ());
77 
78  w->insertAction (w->actions ().back (), action);
79  if (m_separator)
80  w->insertAction (action, m_separator);
81 }
82 
83 template <typename T>
85 { }
86 
87 template <typename T>
88 void
90 {
91  typename T::properties& tp = properties<T> ();
92  QAction *action = qWidget<QAction> ();
93 
94  switch (pId)
95  {
96  case base_properties::ID_VISIBLE:
97  action->setVisible (tp.is_visible ());
98  if (m_separator)
99  m_separator->setVisible (tp.is_visible ());
100  break;
101 
102  case T::properties::ID_TOOLTIPSTRING:
103  action->setToolTip (Utils::fromStdString (tp.get_tooltipstring ()));
104  break;
105 
106  case T::properties::ID_CDATA:
107  {
108  // Get the icon data from cdata or as a named icon
109  QImage img = Utils::makeImageFromCData (tp.get_cdata (), 24, 24);
110 
111  if (img.width () == 0)
112  {
113  QIcon ico;
114  std::string name = tp.get___named_icon__ ();
115  if (! name.empty ())
116  ico = get_icon (name);
117 
118  action->setIcon (ico);
119  }
120  else
121  action->setIcon (QIcon (QPixmap::fromImage (img)));
122  }
123  break;
124 
125  case T::properties::ID_SEPARATOR:
126  if (tp.is_separator ())
127  {
128  if (! m_separator)
129  {
130  m_separator = new QAction (action);
131  m_separator->setSeparator (true);
132  m_separator->setVisible (tp.is_visible ());
133 
134  QWidget *w = qobject_cast<QWidget *> (action->parent ());
135 
136  w->insertAction (action, m_separator);
137  }
138  }
139  else
140  {
141  if (m_separator)
142  delete m_separator;
143  m_separator = nullptr;
144  }
145  break;
146 
147  case T::properties::ID_ENABLE:
148  action->setEnabled (tp.is_enable ());
149  break;
150 
151  default:
152  Object::update (pId);
153  break;
154  }
155 }
156 
157 template <typename T>
158 QIcon ToolBarButton<T>::get_icon (const std::string& name)
159 {
160  return QIcon (global_icon_paths.at (ICON_THEME_OCTAVE) + QString::fromStdString (name) + ".png");
161 }
162 
OCTAVE_END_NAMESPACE(octave)
Definition: Object.h:47
virtual void update(int pId)
Definition: Object.cc:163
~ToolBarButton(void)
void update(int pId)
QIcon get_icon(const std::string &name)
ToolBarButton(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go, QAction *action)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
const QStringList global_icon_paths
@ ICON_THEME_OCTAVE
std::complex< double > w(std::complex< double > z, double relerr=0)
QString fromStdString(const std::string &s)
T::properties & properties(graphics_object obj)
QImage makeImageFromCData(const octave_value &v, int width, int height)