source: src/UIElements/Views/Qt4/QtToolBar_QtFavoriteActions.cpp@ 8859b5

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 8859b5 was 8859b5, checked in by Frederik Heber <heber@…>, 10 years ago

Added QtFavoriteActions to QtToolBar to spawn placeholder icons of most favorite Actions.

  • 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#include "CodePatterns/Observer/Observer.hpp"
49
50#include "Actions/Action.hpp"
51#include "Actions/ActionQueue.hpp"
52#include "Actions/ActionRegistry.hpp"
53#include "Actions/ActionTraits.hpp"
54#include "Actions/OptionTrait.hpp"
55
56using namespace MoleCuilder;
57
58QtToolBar::QtFavoriteActions::QtFavoriteActions() :
59 Observer("QtFavoriteActions"),
60 ActionQueue_observing(false)
61{
62 // initialize counts from settings
63 QSettings settings;
64 settings.beginGroup("FavoriteActions");
65 const ActionQueue::ActionTokens_t actions = ActionQueue::getInstance().getListOfActions();
66 for (ActionQueue::ActionTokens_t::const_iterator tokeniter = actions.begin();
67 tokeniter != actions.end(); ++tokeniter) {
68#ifndef NDEBUG
69 std::pair<ActionCounts_t::iterator, bool> inserter =
70#endif
71 ActionCounts.insert( std::make_pair(
72 *tokeniter,
73 settings.value(QString(tokeniter->c_str()), 0).toInt())
74 );
75 ASSERT( inserter.second,
76 "QtFavoriteActions::QtFavoriteActions() - encountered action token"
77 +*tokeniter+" twice.");
78 }
79
80// resize(settings.value("size", QSize(400, 400)).toSize());
81 settings.endGroup();
82
83 // sign in
84 ActionQueue::getInstance().signOn(this, ActionQueue::ActionQueued);
85 ActionQueue_observing = true;
86}
87
88QtToolBar::QtFavoriteActions::~QtFavoriteActions()
89{
90 // sign off
91 if (ActionQueue_observing)
92 ActionQueue::getInstance().signOff(this, ActionQueue::ActionQueued);
93
94 // store all known counts
95 QSettings settings;
96 settings.beginGroup("FavoriteActions");
97 ActionQueue::ActionTokens_t actions = ActionQueue::getInstance().getListOfActions();
98 for (ActionQueue::ActionTokens_t::const_iterator tokeniter = actions.begin();
99 tokeniter != actions.end(); ++tokeniter) {
100 settings.setValue(QString(tokeniter->c_str()), ActionCounts[*tokeniter]);
101 }
102 settings.endGroup();
103}
104
105void QtToolBar::QtFavoriteActions::update(Observable *publisher)
106{
107 ASSERT(0, "QtFavoriteActions::update() - this should never be called, we are only subscribed to channels.");
108}
109
110void QtToolBar::QtFavoriteActions::subjectKilled(Observable *publisher)
111{
112 ActionQueue_observing = false;
113}
114
115void QtToolBar::QtFavoriteActions::recieveNotification(Observable *publisher, Notification_ptr notification)
116{
117 if (dynamic_cast<ActionQueue *>(publisher) != NULL) {
118 // increment count for this action
119 const Action *lastcalledaction =
120 dynamic_cast<ActionQueue *>(publisher)->lastChanged<Action>();
121 ++ActionCounts[lastcalledaction->getName()];
122 } else {
123 ASSERT(0, "QtFavoriteActions::update() - cannot get here, we are only subscribed to ActionQueue.");
124 }
125}
126
127void
128QtToolBar::QtFavoriteActions::addToolBarActions(
129 QtToolBar &_toolbar,
130 const unsigned int _max) const
131{
132 // invert map
133 std::multimap<unsigned int, std::string> InvertedCounts;
134 for (ActionCounts_t::const_iterator countiter = ActionCounts.begin();
135 countiter != ActionCounts.end(); ++countiter) {
136 InvertedCounts.insert( std::make_pair( countiter->second, countiter->first) );
137 }
138
139 // then use last _max entries
140 std::multimap<unsigned int, std::string>::const_reverse_iterator iter =
141 InvertedCounts.rbegin();
142 ActionQueue &AQ = ActionQueue::getInstance();
143 const present_actions_t &present_actions = _toolbar.getPresentActions();
144 for (size_t added = 0; added<_max; ++iter) {
145 const std::string &token = iter->second;
146 present_actions_t::const_iterator actioniter =
147 present_actions.find(token);
148 if (actioniter == present_actions.end()) {
149 const ActionTrait &traits = AQ.getActionsTrait(token);
150 const std::string icon_name = traits.getMenuName() + std::string("-") + token;
151 const std::string description = traits.getDescription();
152 QIcon icon = getIcon(token, icon_name);
153 _toolbar.addActionItem(
154 token,
155 description,
156 icon_name);
157 ++added;
158 }
159 }
160}
161
162QIcon
163QtToolBar::QtFavoriteActions::createIconPlaceholder(
164 const std::string &_token
165 ) const
166{
167 LOG(2, "DEBUG: Creating icon with text " << _token);
168 QPixmap pixmap(100,100);
169 pixmap.fill(QColor("white"));
170
171 QPainter painter( &pixmap );
172 QRect rectangle( QPoint(0,0), QPoint(100,100));
173 QRect boundingbox = QRect(1,1, 98, 98 );
174 painter.drawRect( boundingbox );
175 QFont font = QFont("Arial");
176 font.setPixelSize(30);
177 painter.setFont( font );
178 QString icon_text(_token.c_str());
179 icon_text.replace(QString("-"), QString("\n"));
180 painter.drawText( rectangle, Qt::TextWordWrap, icon_text, &boundingbox );
181
182 QIcon PlaceholderIcon(pixmap);
183
184 return PlaceholderIcon;
185}
186
187QIcon
188QtToolBar::QtFavoriteActions::getIcon(
189 const std::string &_token,
190 const std::string &_icon_name
191 ) const
192{
193 LOG(2, "DEBUG: Obtaining icon with text " << _token);
194 // TODO: look up _icon_name from QIcon::fromTheme or someplace
195 if (QIcon::hasThemeIcon(QString(_icon_name.c_str())))
196 return QIcon::fromTheme(QString(_icon_name.c_str()));
197 else
198 return createIconPlaceholder(_token);
199}
Note: See TracBrowser for help on using the repository browser.