source: src/UIElements/Views/Qt4/QtToolBar_QtFavoriteActions.cpp@ 534374

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 534374 was c01fec, checked in by Frederik Heber <heber@…>, 10 years ago

Undo/Redobuttons are grayed out when respective Action not possible.

  • Property mode set to 100644
File size: 6.1 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2014 Frederik Heber. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * QtFavoriteActions.cpp
25 *
26 * Created on: Sep 11, 2014
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include <Qt/qaction.h>
36#include <Qt/qpainter.h>
37#include <Qt/qpixmap.h>
38#include <Qt/qsettings.h>
39
40#include "UIElements/Views/Qt4/QtToolBar.hpp"
41
42#include "CodePatterns/MemDebug.hpp"
43
44#include <string>
45
46#include "CodePatterns/Assert.hpp"
47#include "CodePatterns/Log.hpp"
48
49#include "Actions/Action.hpp"
50#include "Actions/ActionQueue.hpp"
51#include "Actions/ActionRegistry.hpp"
52#include "Actions/ActionTraits.hpp"
53#include "Actions/OptionTrait.hpp"
54
55using namespace MoleCuilder;
56
57QtToolBar::QtFavoriteActions::QtFavoriteActions() :
58 Observer("QtFavoriteActions"),
59 ActionQueue_observing(false)
60{
61 // initialize counts from settings
62 QSettings settings;
63 settings.beginGroup("FavoriteActions");
64 const ActionQueue::ActionTokens_t actions = ActionQueue::getInstance().getListOfActions();
65 for (ActionQueue::ActionTokens_t::const_iterator tokeniter = actions.begin();
66 tokeniter != actions.end(); ++tokeniter) {
67#ifndef NDEBUG
68 std::pair<ActionCounts_t::iterator, bool> inserter =
69#endif
70 ActionCounts.insert( std::make_pair(
71 *tokeniter,
72 settings.value(QString(tokeniter->c_str()), 0).toInt())
73 );
74 ASSERT( inserter.second,
75 "QtFavoriteActions::QtFavoriteActions() - encountered action token"
76 +*tokeniter+" twice.");
77 }
78
79// resize(settings.value("size", QSize(400, 400)).toSize());
80 settings.endGroup();
81
82 // sign in
83 ActionQueue::getInstance().signOn(this, ActionQueue::ActionQueued);
84 ActionQueue_observing = true;
85}
86
87QtToolBar::QtFavoriteActions::~QtFavoriteActions()
88{
89 // sign off
90 if (ActionQueue_observing)
91 ActionQueue::getInstance().signOff(this, ActionQueue::ActionQueued);
92
93 // store all known counts
94 QSettings settings;
95 settings.beginGroup("FavoriteActions");
96 ActionQueue::ActionTokens_t actions = ActionQueue::getInstance().getListOfActions();
97 for (ActionQueue::ActionTokens_t::const_iterator tokeniter = actions.begin();
98 tokeniter != actions.end(); ++tokeniter) {
99 settings.setValue(QString(tokeniter->c_str()), ActionCounts[*tokeniter]);
100 }
101 settings.endGroup();
102}
103
104void QtToolBar::QtFavoriteActions::update(Observable *publisher)
105{
106 ASSERT(0, "QtFavoriteActions::update() - this should never be called, we are only subscribed to channels.");
107}
108
109void QtToolBar::QtFavoriteActions::subjectKilled(Observable *publisher)
110{
111 ActionQueue_observing = false;
112}
113
114void QtToolBar::QtFavoriteActions::recieveNotification(Observable *publisher, Notification_ptr notification)
115{
116 if (dynamic_cast<ActionQueue *>(publisher) != NULL) {
117 // increment count for this action
118 const Action *lastcalledaction =
119 dynamic_cast<ActionQueue *>(publisher)->lastChanged<Action>();
120 ++ActionCounts[lastcalledaction->getName()];
121 } else {
122 ASSERT(0, "QtFavoriteActions::update() - cannot get here, we are only subscribed to ActionQueue.");
123 }
124}
125
126void
127QtToolBar::QtFavoriteActions::addToolBarActions(
128 QtToolBar &_toolbar,
129 const unsigned int _max) const
130{
131 // invert map
132 std::multimap<unsigned int, std::string> InvertedCounts;
133 for (ActionCounts_t::const_iterator countiter = ActionCounts.begin();
134 countiter != ActionCounts.end(); ++countiter) {
135 InvertedCounts.insert( std::make_pair( countiter->second, countiter->first) );
136 }
137
138 // then use last _max entries
139 std::multimap<unsigned int, std::string>::const_reverse_iterator iter =
140 InvertedCounts.rbegin();
141 ActionQueue &AQ = ActionQueue::getInstance();
142 const present_actions_t &present_actions = _toolbar.getPresentActions();
143 for (size_t added = 0; added<_max; ++iter) {
144 const std::string &token = iter->second;
145 present_actions_t::const_iterator actioniter =
146 present_actions.find(token);
147 if (actioniter == present_actions.end()) {
148 const ActionTrait &traits = AQ.getActionsTrait(token);
149 const std::string icon_name = traits.getMenuName() + std::string("-") + token;
150 const std::string description = traits.getDescription();
151 QIcon icon = getIcon(token, icon_name);
152 _toolbar.addActionItem(
153 token,
154 description,
155 icon_name);
156 ++added;
157 }
158 }
159}
160
161QIcon
162QtToolBar::QtFavoriteActions::createIconPlaceholder(
163 const std::string &_token
164 ) const
165{
166 LOG(2, "DEBUG: Creating icon with text " << _token);
167 QPixmap pixmap(100,100);
168 pixmap.fill(QColor("white"));
169
170 QPainter painter( &pixmap );
171 QRect rectangle( QPoint(0,0), QPoint(100,100));
172 QRect boundingbox = QRect(1,1, 98, 98 );
173 painter.drawRect( boundingbox );
174 QFont font = QFont("Arial");
175 font.setPixelSize(30);
176 painter.setFont( font );
177 QString icon_text(_token.c_str());
178 icon_text.replace(QString("-"), QString("\n"));
179 painter.drawText( rectangle, Qt::TextWordWrap, icon_text, &boundingbox );
180
181 QIcon PlaceholderIcon(pixmap);
182
183 return PlaceholderIcon;
184}
185
186QIcon
187QtToolBar::QtFavoriteActions::getIcon(
188 const std::string &_token,
189 const std::string &_icon_name
190 ) const
191{
192 LOG(2, "DEBUG: Obtaining icon with text " << _token);
193 // TODO: look up _icon_name from QIcon::fromTheme or someplace
194 if (QIcon::hasThemeIcon(QString(_icon_name.c_str())))
195 return QIcon::fromTheme(QString(_icon_name.c_str()));
196 else
197 return createIconPlaceholder(_token);
198}
Note: See TracBrowser for help on using the repository browser.