1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-23 01:53:53 +00:00
openmw/apps/opencs/view/widget/scenetooltoggle2.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

151 lines
3.9 KiB
C++
Raw Normal View History

2014-11-28 08:14:02 +00:00
#include "scenetooltoggle2.hpp"
#include <sstream>
2022-10-19 17:02:00 +00:00
#include <type_traits>
#include <utility>
2014-11-28 08:14:02 +00:00
#include <QFrame>
#include <QHBoxLayout>
#include <QIcon>
2022-10-19 17:02:00 +00:00
#include <apps/opencs/view/widget/scenetool.hpp>
2014-11-28 08:14:02 +00:00
#include "pushbutton.hpp"
#include "scenetoolbar.hpp"
void CSVWidget::SceneToolToggle2::adjustToolTip()
{
QString toolTip = mToolTip;
toolTip += "<p>Currently enabled: ";
bool first = true;
for (std::map<PushButton*, ButtonDesc>::const_iterator iter(mButtons.begin()); iter != mButtons.end(); ++iter)
if (iter->first->isChecked())
{
if (!first)
toolTip += ", ";
else
first = false;
toolTip += iter->second.mName;
}
if (first)
toolTip += "none";
toolTip += "<p>(left click to alter selection)";
setToolTip(toolTip);
}
void CSVWidget::SceneToolToggle2::adjustIcon()
{
unsigned int buttonIds = 0;
for (std::map<PushButton*, ButtonDesc>::const_iterator iter(mButtons.begin()); iter != mButtons.end(); ++iter)
if (iter->first->isChecked())
buttonIds |= iter->second.mButtonId;
2014-11-28 08:14:02 +00:00
std::ostringstream stream;
stream << mCompositeIcon << buttonIds;
2014-11-28 08:14:02 +00:00
setIcon(QIcon(QString::fromUtf8(stream.str().c_str())));
}
CSVWidget::SceneToolToggle2::SceneToolToggle2(
SceneToolbar* parent, const QString& toolTip, const std::string& compositeIcon, const std::string& singleIcon)
: SceneTool(parent)
, mCompositeIcon(compositeIcon)
, mSingleIcon(singleIcon)
, mButtonSize(parent->getButtonSize())
, mIconSize(parent->getIconSize())
, mToolTip(toolTip)
2020-11-13 07:39:47 +00:00
, mFirst(nullptr)
2014-11-28 08:14:02 +00:00
{
mPanel = new QFrame(this, Qt::Popup);
mLayout = new QHBoxLayout(mPanel);
mLayout->setContentsMargins(QMargins(0, 0, 0, 0));
mPanel->setLayout(mLayout);
}
void CSVWidget::SceneToolToggle2::showPanel(const QPoint& position)
{
mPanel->move(position);
mPanel->show();
if (mFirst)
mFirst->setFocus(Qt::OtherFocusReason);
}
void CSVWidget::SceneToolToggle2::addButton(
unsigned int id, unsigned int mask, const QString& name, const QString& tooltip, bool disabled)
2014-11-28 08:14:02 +00:00
{
std::ostringstream stream;
stream << mSingleIcon << id;
2014-11-28 08:14:02 +00:00
PushButton* button = new PushButton(
QIcon(QPixmap(stream.str().c_str())), PushButton::Type_Toggle, tooltip.isEmpty() ? name : tooltip, mPanel);
button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
button->setIconSize(QSize(mIconSize, mIconSize));
button->setFixedSize(mButtonSize, mButtonSize);
if (disabled)
button->setDisabled(true);
2014-11-28 08:14:02 +00:00
mLayout->addWidget(button);
ButtonDesc desc;
desc.mButtonId = id;
desc.mMask = mask;
2014-11-28 08:14:02 +00:00
desc.mName = name;
2021-05-02 06:43:44 +00:00
desc.mIndex = static_cast<int>(mButtons.size());
2014-11-28 08:14:02 +00:00
mButtons.insert(std::make_pair(button, desc));
connect(button, &QPushButton::clicked, this, &SceneToolToggle2::selected);
2014-11-28 08:14:02 +00:00
if (mButtons.size() == 1 && !disabled)
2014-11-28 08:14:02 +00:00
mFirst = button;
}
unsigned int CSVWidget::SceneToolToggle2::getSelectionMask() const
2014-11-28 08:14:02 +00:00
{
unsigned int selection = 0;
for (std::map<PushButton*, ButtonDesc>::const_iterator iter(mButtons.begin()); iter != mButtons.end(); ++iter)
if (iter->first->isChecked())
selection |= iter->second.mMask;
2014-11-28 08:14:02 +00:00
return selection;
}
void CSVWidget::SceneToolToggle2::setSelectionMask(unsigned int selection)
2014-11-28 08:14:02 +00:00
{
for (std::map<PushButton*, ButtonDesc>::iterator iter(mButtons.begin()); iter != mButtons.end(); ++iter)
iter->first->setChecked(selection & iter->second.mMask);
2014-11-28 08:14:02 +00:00
adjustToolTip();
adjustIcon();
}
void CSVWidget::SceneToolToggle2::selected()
{
std::map<PushButton*, ButtonDesc>::const_iterator iter = mButtons.find(dynamic_cast<PushButton*>(sender()));
if (iter != mButtons.end())
{
if (!iter->first->hasKeepOpen())
mPanel->hide();
adjustToolTip();
adjustIcon();
emit selectionChanged();
}
}