Qt editor: Fix viewport titles and decorations

The custom title bar caused a lot of problems. Particularly turning it on and off between docked and undocked modes caused some internal trouble with the rendering going off. It also didn't have a close button.

Fortunately, the desired font and highlight effect of selected viewports can also be achieved with style sheets on the dock.
qteditor
Sven Eberhardt 2016-08-06 23:39:44 -04:00
parent a993b6e1ec
commit 5c70c2aa42
2 changed files with 22 additions and 62 deletions

View File

@ -29,6 +29,7 @@
#include "gui/C4MouseControl.h"
#include "landscape/C4Landscape.h"
#include "object/C4GameObjects.h"
#include "player/C4PlayerList.h"
/* Console viewports */
@ -466,46 +467,16 @@ void C4ConsoleQtViewportScrollArea::setScrollBarVisibility(bool visible)
}
}
C4ConsoleQtViewportLabel::C4ConsoleQtViewportLabel(const QString &title, C4ConsoleQtViewportDockWidget *dock)
: QLabel(dock), dock(dock)
{
OnActiveChanged(false);
setText(title);
}
void C4ConsoleQtViewportLabel::OnActiveChanged(bool active)
{
// set color schemes for inactive / active viewport headers
QColor bgclr = QApplication::palette(this).color(QPalette::Highlight);
QColor fontclr = QApplication::palette(this).color(QPalette::HighlightedText);
if (active)
setStyleSheet(QString(
"QLabel { background: %1; padding: 3px; color: %2; font-weight: bold; }")
.arg(bgclr.name(), fontclr.name()));
else
setStyleSheet(QString(
"QLabel { padding: 3px; }"));
}
void C4ConsoleQtViewportLabel::mousePressEvent(QMouseEvent *eventPress)
{
dock->view->setFocus();
QLabel::mousePressEvent(eventPress);
}
C4ConsoleQtViewportDockWidget::C4ConsoleQtViewportDockWidget(C4ConsoleQtMainWindow *main_window, QMainWindow *parent, C4ViewportWindow *cvp)
: QDockWidget("", parent), main_window(main_window), cvp(cvp)
{
// Translated title
setWindowTitle(LoadResStr("IDS_CNS_VIEWPORT"));
// Translated title or player name
C4Player *plr = ::Players.Get(cvp->cvp->GetPlayer());
setWindowTitle(plr ? plr->GetName() : LoadResStr("IDS_CNS_VIEWPORT"));
// Actual view container, wrapped in scrolling area
auto scrollarea = new C4ConsoleQtViewportScrollArea(this);
view = new C4ConsoleQtViewportView(scrollarea);
scrollarea->setViewport(view);
setTitleBarWidget((title_label = new C4ConsoleQtViewportLabel(windowTitle(), this)));
setWidget(scrollarea);
connect(this, SIGNAL(topLevelChanged(bool)), this, SLOT(TopLevelChanged(bool)));
// Register viewport widget for periodic rendering updates.
@ -521,21 +492,20 @@ void C4ConsoleQtViewportDockWidget::mousePressEvent(QMouseEvent *eventPress)
void C4ConsoleQtViewportDockWidget::OnActiveChanged(bool active)
{
title_label->OnActiveChanged(active);
// Title bar of the selected viewport should be drawn in highlight colors to show that keyboard input will now go to the viewport.
// Unfortunately, color and font of the title is not taken from QDockWidget::title but directly from QDockWidget.
// Provide them in both just in case Qt ever changes its definition.
QColor bgclr = QApplication::palette(this).color(QPalette::Highlight);
QColor fontclr = QApplication::palette(this).color(QPalette::HighlightedText);
if (active)
setStyleSheet(QString(
"QDockWidget::title { text-align: left; background: %1; padding-left: 3px; color: %2; font-weight: bold; } QDockWidget { color: %2; font-weight: bold; }").arg(bgclr.name(), fontclr.name()));
else
setStyleSheet(QString());
}
void C4ConsoleQtViewportDockWidget::TopLevelChanged(bool is_floating)
{
if (!is_floating)
{
// docked has custom title
setTitleBarWidget(title_label);
}
else
{
// undocked using OS title
setTitleBarWidget(NULL);
}
// Ensure focus after undock and after re-docking floating viewport window
view->setFocus();
}
@ -550,3 +520,10 @@ void C4ConsoleQtViewportDockWidget::closeEvent(QCloseEvent * event)
deleteLater();
}
}
bool C4ConsoleQtViewportDockWidget::event(QEvent *e)
{
// Focus on title bar click
if (e->type() == QEvent::NonClientAreaMouseButtonPress || e->type() == QEvent::MouseButtonPress) view->setFocus();
return QDockWidget::event(e);
}

View File

@ -80,22 +80,6 @@ public:
friend C4ConsoleQtViewportView;
};
class C4ConsoleQtViewportLabel : public QLabel
{
Q_OBJECT
class C4ConsoleQtViewportDockWidget *dock;
bool active;
QPalette pal_inactive, pal_active;
public:
C4ConsoleQtViewportLabel(const QString &title, C4ConsoleQtViewportDockWidget *dock);
void OnActiveChanged(bool active);
protected:
void mousePressEvent(QMouseEvent *eventPress) override;
};
class C4ConsoleQtViewportDockWidget : public QDockWidget
{
Q_OBJECT
@ -103,7 +87,6 @@ class C4ConsoleQtViewportDockWidget : public QDockWidget
class C4ConsoleQtMainWindow *main_window;
class C4ViewportWindow *cvp;
C4ConsoleQtViewportView *view;
C4ConsoleQtViewportLabel *title_label;
protected:
void mousePressEvent(QMouseEvent *eventPress);
@ -114,13 +97,13 @@ public:
virtual void closeEvent(QCloseEvent * event);
class C4ViewportWindow *GetViewportWindow() const { return cvp; }
void SetFocus() { view->setFocus(); } // forward focus to view
bool event(QEvent *e) override;
private slots :
void TopLevelChanged(bool is_floating);
friend C4ConsoleQtViewportView;
friend C4ConsoleQtViewportScrollArea;
friend C4ConsoleQtViewportLabel;
};