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