[8859b5] | 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 |
|
---|
| 56 | using namespace MoleCuilder;
|
---|
| 57 |
|
---|
| 58 | QtToolBar::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 |
|
---|
| 88 | QtToolBar::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 |
|
---|
| 105 | void QtToolBar::QtFavoriteActions::update(Observable *publisher)
|
---|
| 106 | {
|
---|
| 107 | ASSERT(0, "QtFavoriteActions::update() - this should never be called, we are only subscribed to channels.");
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | void QtToolBar::QtFavoriteActions::subjectKilled(Observable *publisher)
|
---|
| 111 | {
|
---|
| 112 | ActionQueue_observing = false;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | void 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 |
|
---|
| 127 | void
|
---|
| 128 | QtToolBar::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 |
|
---|
| 162 | QIcon
|
---|
| 163 | QtToolBar::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 |
|
---|
| 187 | QIcon
|
---|
| 188 | QtToolBar::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 | }
|
---|