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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/*
SPDX-FileCopyrightText: 2007 Roberto Raggi <roberto@kdevelop.org>
SPDX-FileCopyrightText: 2007 Hamish Rodda <rodda@kde.org>
SPDX-FileCopyrightText: 2011 Alexander Dymo <adymo@kdevelop.org>
SPDX-License-Identifier: LicenseRef-MIT-KDevelop-Ideal
*/
#include "idealbuttonbarwidget.h"
#include "mainwindow.h"
#include "idealdockwidget.h"
#include "ideallayout.h"
#include "idealtoolbutton.h"
#include "document.h"
#include "view.h"
#include <KLocalizedString>
#include <KSharedConfig>
#include <KConfigGroup>
#include <QBoxLayout>
#include <QApplication>
using namespace Sublime;
class ToolViewAction : public QAction
{
Q_OBJECT
public:
ToolViewAction(IdealDockWidget *dock, QObject* parent) : QAction(parent), m_dock(dock)
{
setCheckable(true);
const QString title = dock->view()->document()->title();
setIcon(dock->windowIcon());
setToolTip(i18nc("@info:tooltip", "Toggle '%1' tool view", title));
setText(title);
//restore tool view shortcut config
KConfigGroup config = KSharedConfig::openConfig()->group("UI");
QStringList shortcutStrings = config.readEntry(QStringLiteral("Shortcut for %1").arg(title), QStringList());
setShortcuts({ QKeySequence::fromString(shortcutStrings.value(0)), QKeySequence::fromString(shortcutStrings.value(1)) });
dock->setWindowTitle(title);
dock->view()->widget()->installEventFilter(this);
refreshText();
}
IdealDockWidget *dockWidget() const
{
Q_ASSERT(m_dock);
return m_dock;
}
IdealToolButton* button() { return m_button; }
void setButton(IdealToolButton* button) {
m_button = button;
refreshText();
}
QString id() {
return m_dock->view()->document()->documentSpecifier();
}
private:
bool eventFilter(QObject * watched, QEvent * event) override
{
// an event may arrive when m_dock->view()->widget() is already destroyed
// so check for event type first.
if (event->type() == QEvent::EnabledChange && watched == m_dock->view()->widget()) {
refreshText();
}
return QAction::eventFilter(watched, event);
}
void refreshText()
{
const auto widget = m_dock->view()->widget();
const QString title = m_dock->view()->document()->title();
setText(widget->isEnabled() ? title : QStringLiteral("(%1)").arg(title));
}
QPointer<IdealDockWidget> m_dock;
QPointer<IdealToolButton> m_button;
};
IdealButtonBarWidget::IdealButtonBarWidget(Qt::DockWidgetArea area,
IdealController *controller, Sublime::MainWindow *parent)
: QWidget(parent)
, m_area(area)
, m_controller(controller)
, m_corner(nullptr)
, m_showState(false)
, m_buttonsLayout(nullptr)
{
setContextMenuPolicy(Qt::CustomContextMenu);
setToolTip(i18nc("@info:tooltip", "Right click to add new tool views."));
m_buttonsLayout = new IdealButtonBarLayout(orientation(), this);
if (area == Qt::BottomDockWidgetArea)
{
auto *statusLayout = new QBoxLayout(QBoxLayout::LeftToRight, this);
statusLayout->setContentsMargins(0, 0, 0, 0);
statusLayout->addLayout(m_buttonsLayout);
statusLayout->addStretch(1);
m_corner = new QWidget(this);
auto *cornerLayout = new QBoxLayout(QBoxLayout::LeftToRight, m_corner);
cornerLayout->setContentsMargins(0, 0, 0, 0);
cornerLayout->setSpacing(0);
statusLayout->addWidget(m_corner);
} else {
auto *superLayout = new QBoxLayout(QBoxLayout::TopToBottom, this);
superLayout->setContentsMargins(0, 0, 0, 0);
superLayout->addLayout(m_buttonsLayout);
superLayout->addStretch(1);
}
}
QAction* IdealButtonBarWidget::addWidget(IdealDockWidget *dock,
Area *area, View *view)
{
if (m_area == Qt::BottomDockWidgetArea || m_area == Qt::TopDockWidgetArea)
dock->setFeatures( dock->features() | QDockWidget::DockWidgetVerticalTitleBar );
dock->setArea(area);
dock->setView(view);
dock->setDockWidgetArea(m_area);
auto action = new ToolViewAction(dock, this);
addAction(action);
return action;
}
QWidget* IdealButtonBarWidget::corner() const
{
return m_corner;
}
void IdealButtonBarWidget::addAction(QAction* qaction)
{
QWidget::addAction(qaction);
auto action = qobject_cast<ToolViewAction*>(qaction);
if (!action || action->button()) {
return;
}
bool wasEmpty = isEmpty();
auto *button = new IdealToolButton(m_area);
//apol: here we set the usual width of a button for the vertical toolbars as the minimumWidth
//this is done because otherwise when we remove all the buttons and re-add new ones we get all
//the screen flickering. This is solved by not defaulting to a smaller width when it's empty
int w = button->sizeHint().width();
if (orientation() == Qt::Vertical && w > minimumWidth()) {
setMinimumWidth(w);
}
action->setButton(button);
button->setDefaultAction(action);
Q_ASSERT(action->dockWidget());
connect(action, &QAction::toggled, this, QOverload<bool>::of(&IdealButtonBarWidget::showWidget));
connect(button, &IdealToolButton::customContextMenuRequested,
action->dockWidget(), &IdealDockWidget::contextMenuRequested);
addButtonToOrder(button);
applyOrderToLayout();
if (wasEmpty) {
emit emptyChanged();
}
}
void IdealButtonBarWidget::removeAction(QAction* widgetAction)
{
QWidget::removeAction(widgetAction);
auto action = static_cast<ToolViewAction*>(widgetAction);
action->button()->deleteLater();
delete action;
if (m_buttonsLayout->isEmpty()) {
emit emptyChanged();
}
}
bool IdealButtonBarWidget::isEmpty() const
{
return actions().isEmpty();
}
bool IdealButtonBarWidget::isShown() const
{
const auto actions = this->actions();
return std::any_of(actions.cbegin(), actions.cend(),
[](const QAction* action){ return action->isChecked(); });
}
void IdealButtonBarWidget::saveShowState()
{
m_showState = isShown();
}
bool IdealButtonBarWidget::lastShowState()
{
return m_showState;
}
QString IdealButtonBarWidget::id(const IdealToolButton* button) const
{
const auto actions = this->actions();
for (QAction* a : actions) {
auto tva = qobject_cast<ToolViewAction*>(a);
if (tva && tva->button() == button) {
return tva->id();
}
}
return QString();
}
IdealToolButton* IdealButtonBarWidget::button(const QString& id) const
{
const auto actions = this->actions();
for (QAction* a : actions) {
auto tva = qobject_cast<ToolViewAction*>(a);
if (tva && tva->id() == id) {
return tva->button();
}
}
return nullptr;
}
void IdealButtonBarWidget::addButtonToOrder(const IdealToolButton* button)
{
QString buttonId = id(button);
if (!m_buttonsOrder.contains(buttonId)) {
m_buttonsOrder.push_back(buttonId);
}
}
void IdealButtonBarWidget::loadOrderSettings(const KConfigGroup& configGroup)
{
m_buttonsOrder = configGroup.readEntry(QStringLiteral("(%1) Tool Views Order").arg(m_area), QStringList());
applyOrderToLayout();
}
void IdealButtonBarWidget::saveOrderSettings(KConfigGroup& configGroup)
{
takeOrderFromLayout();
configGroup.writeEntry(QStringLiteral("(%1) Tool Views Order").arg(m_area), m_buttonsOrder);
}
bool IdealButtonBarWidget::isLocked() const
{
KConfigGroup config = KSharedConfig::openConfig()->group("UI");
return config.readEntry(QStringLiteral("Toolview Bar (%1) Is Locked").arg(m_area), false);
}
void IdealButtonBarWidget::applyOrderToLayout()
{
// If widget already have some buttons in the layout then calling loadOrderSettings() may leads
// to situations when loaded order does not contains all existing buttons. Therefore we should
// fix this with using addToOrder() method.
for (int i = 0; i < m_buttonsLayout->count(); ++i) {
if (auto button = qobject_cast<IdealToolButton*>(m_buttonsLayout->itemAt(i)->widget())) {
addButtonToOrder(button);
m_buttonsLayout->removeWidget(button);
--i;
}
}
for (const QString& id : qAsConst(m_buttonsOrder)) {
if (auto b = button(id)) {
m_buttonsLayout->addWidget(b);
}
}
}
void IdealButtonBarWidget::takeOrderFromLayout()
{
m_buttonsOrder.clear();
for (int i = 0; i < m_buttonsLayout->count(); ++i) {
if (auto button = qobject_cast<IdealToolButton*>(m_buttonsLayout->itemAt(i)->widget())) {
m_buttonsOrder += id(button);
}
}
}
Qt::Orientation IdealButtonBarWidget::orientation() const
{
if (m_area == Qt::LeftDockWidgetArea || m_area == Qt::RightDockWidgetArea)
return Qt::Vertical;
return Qt::Horizontal;
}
Qt::DockWidgetArea IdealButtonBarWidget::area() const
{
return m_area;
}
void IdealButtonBarWidget::showWidget(bool checked)
{
Q_ASSERT(parentWidget() != nullptr);
auto *action = qobject_cast<QAction *>(sender());
Q_ASSERT(action);
showWidget(action, checked);
}
void IdealButtonBarWidget::showWidget(QAction *action, bool checked)
{
auto widgetAction = static_cast<ToolViewAction*>(action);
IdealToolButton* button = widgetAction->button();
Q_ASSERT(button);
if (checked) {
if ( !QApplication::keyboardModifiers().testFlag(Qt::ControlModifier) ) {
// Make sure only one widget is visible at any time.
// The alternative to use a QActionCollection and setting that to "exclusive"
// has a big drawback: QActions in a collection that is exclusive cannot
// be un-checked by the user, e.g. in the View -> Tool Views menu.
const auto actions = this->actions();
for (QAction* otherAction : actions) {
if ( otherAction != widgetAction && otherAction->isChecked() )
otherAction->setChecked(false);
}
}
m_controller->lastDockWidget[m_area] = widgetAction->dockWidget();
}
m_controller->showDockWidget(widgetAction->dockWidget(), checked);
widgetAction->setChecked(checked);
button->setChecked(checked);
}
IdealDockWidget * IdealButtonBarWidget::widgetForAction(QAction *_action) const
{
return static_cast<ToolViewAction*>(_action)->dockWidget();
}
#include "idealbuttonbarwidget.moc"
|