File: scenetoolmode.cpp

package info (click to toggle)
openmw 0.50.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,076 kB
  • sloc: cpp: 380,958; xml: 2,192; sh: 1,449; python: 911; makefile: 26; javascript: 5
file content (170 lines) | stat: -rw-r--r-- 4,243 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "scenetoolmode.hpp"

#include <QContextMenuEvent>
#include <QEvent>
#include <QFrame>
#include <QHBoxLayout>
#include <QIcon>
#include <QMenu>

#include <apps/opencs/view/widget/pushbutton.hpp>
#include <apps/opencs/view/widget/scenetool.hpp>

#include <components/misc/scalableicon.hpp>

#include <type_traits>
#include <utility>

#include "modebutton.hpp"
#include "scenetoolbar.hpp"

void CSVWidget::SceneToolMode::contextMenuEvent(QContextMenuEvent* event)
{
    QMenu menu(this);
    if (createContextMenu(&menu))
        menu.exec(event->globalPos());
}

bool CSVWidget::SceneToolMode::createContextMenu(QMenu* menu)
{
    if (mCurrent)
        return mCurrent->createContextMenu(menu);

    return false;
}

void CSVWidget::SceneToolMode::adjustToolTip(const ModeButton* activeMode)
{
    QString toolTip = mToolTip;

    toolTip += "<p>Currently selected: " + activeMode->getBaseToolTip();

    toolTip += "<p>(left click to change mode)";

    if (createContextMenu(nullptr))
        toolTip += "<br>(right click to access context menu)";

    setToolTip(toolTip);
}

void CSVWidget::SceneToolMode::setButton(std::map<ModeButton*, std::string>::iterator iter)
{
    for (std::map<ModeButton*, std::string>::const_iterator iter2 = mButtons.begin(); iter2 != mButtons.end(); ++iter2)
        iter2->first->setChecked(iter2 == iter);

    setIcon(iter->first->icon());
    adjustToolTip(iter->first);

    if (mCurrent != iter->first)
    {
        if (mCurrent)
            mCurrent->deactivate(mToolbar);

        mCurrent = iter->first;
        mCurrent->activate(mToolbar);
    }

    emit modeChanged(iter->second);
}

CSVWidget::SceneToolMode::SceneToolMode(SceneToolbar* parent, const QString& toolTip)
    : SceneTool(parent)
    , mButtonSize(parent->getButtonSize())
    , mIconSize(parent->getIconSize())
    , mToolTip(toolTip)
    , mFirst(nullptr)
    , mCurrent(nullptr)
    , mToolbar(parent)
{
    mPanel = new QFrame(this, Qt::Popup);

    mLayout = new QHBoxLayout(mPanel);

    mLayout->setContentsMargins(QMargins(0, 0, 0, 0));

    mPanel->setLayout(mLayout);
}

void CSVWidget::SceneToolMode::showPanel(const QPoint& position)
{
    mPanel->move(position);
    mPanel->show();

    if (mFirst)
        mFirst->setFocus(Qt::OtherFocusReason);
}

void CSVWidget::SceneToolMode::addButton(const std::string& icon, const std::string& id, const QString& tooltip)
{
    ModeButton* button = new ModeButton(Misc::ScalableIcon::load(icon.c_str()), tooltip, mPanel);
    addButton(button, id);
}

void CSVWidget::SceneToolMode::addButton(ModeButton* button, const std::string& id)
{
    button->setParent(mPanel);

    button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    button->setIconSize(QSize(mIconSize, mIconSize));
    button->setFixedSize(mButtonSize, mButtonSize);

    mLayout->addWidget(button);

    mButtons.insert(std::make_pair(button, id));

    connect(button, &ModeButton::clicked, this, &SceneToolMode::selected);

    if (mButtons.size() == 1)
    {
        mFirst = mCurrent = button;
        setIcon(button->icon());
        button->setChecked(true);
        adjustToolTip(button);
        mCurrent->activate(mToolbar);
    }
}

CSVWidget::ModeButton* CSVWidget::SceneToolMode::getCurrent()
{
    return mCurrent;
}

std::string CSVWidget::SceneToolMode::getCurrentId() const
{
    auto currentButton = mButtons.find(mCurrent);
    assert(currentButton != mButtons.end());
    return currentButton->second;
}

void CSVWidget::SceneToolMode::setButton(const std::string& id)
{
    for (std::map<ModeButton*, std::string>::iterator iter = mButtons.begin(); iter != mButtons.end(); ++iter)
        if (iter->second == id)
        {
            setButton(iter);
            break;
        }
}

bool CSVWidget::SceneToolMode::event(QEvent* event)
{
    if (event->type() == QEvent::ToolTip)
    {
        adjustToolTip(mCurrent);
    }

    return SceneTool::event(event);
}

void CSVWidget::SceneToolMode::selected()
{
    std::map<ModeButton*, std::string>::iterator iter = mButtons.find(dynamic_cast<ModeButton*>(sender()));

    if (iter != mButtons.end())
    {
        if (!iter->first->hasKeepOpen())
            mPanel->hide();

        setButton(iter);
    }
}