Files
c-evo-evo/widget/src/CevoDrawDlg.cpp
T

227 lines
7.9 KiB
C++

// This file is part of c-evo-evo.
//
// This software is governed by the CeCILL license under French law and
// abiding by the rules of distribution of free software. You can use,
// modify and/or redistribute the software under the terms of the CeCILL
// license as circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy,
// modify and redistribute granted by the license, users are provided only
// with a limited warranty and the software's author, the holder of the
// economic rights, and the successive licensors have only limited
// liability.
//
// In this respect, the user's attention is drawn to the risks associated
// with loading, using, modifying and/or developing or reproducing the
// software by the user in light of its specific status of free software,
// that may mean that it is complicated to manipulate, and that also
// therefore means that it is reserved for developers and experienced
// professionals having in-depth computer knowledge. Users are therefore
// encouraged to load and test the software's suitability as regards their
// requirements in conditions enabling the security of their systems and/or
// data to be ensured and, more generally, to use and operate it in the
// same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
#include "Cevo/CevoDrawDlg.h"
#include "Cevo/CevoArea.h"
#include "Cevo/CevoButtonA.h"
#include "Cevo/CevoButtonB.h"
#include "Cevo/CevoButtonBase.h"
#include "Cevo/CevoGraphicSet.h"
#include "Cevo/CevoScreenTools.h"
#include "Cevo/CevoTexture.h"
#include <QKeyEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QRegion>
#include <QWindow>
namespace Cevo {
DrawDlg::DrawDlg(QWidget *parent) : Dpi::Form(parent)
{
// The original's dialogs are borderless (BorderStyle = bsNone): they draw their own frame and title.
setWindowFlag(Qt::FramelessWindowHint, true);
setMouseTracking(true); // the dialogs highlight items on mouse over, like the VCL's OnMouseMove
QPalette pal = palette();
pal.setColor(QPalette::Window, Qt::black);
setPalette(pal);
setAutoFillBackground(true);
}
void DrawDlg::mousePressEvent(QMouseEvent *event)
{
const QPoint pos = event->pos();
if (event->button() == Qt::LeftButton && rect().contains(pos) && pos.y() < titleHeight) {
// The compositor moves the window: programmatic moves are ignored on Wayland.
if (windowHandle() && windowHandle()->startSystemMove()) {
m_moveActive = false;
return;
}
m_moveMousePos = mapToGlobal(event->pos());
m_moveFormPos = QPoint(x(), y());
m_moveActive = true;
} else {
m_moveActive = false;
}
}
void DrawDlg::mouseMoveEvent(QMouseEvent *event)
{
if (m_moveActive) {
const QPoint delta = mapToGlobal(event->pos()) - m_moveMousePos;
move(m_moveFormPos + delta);
}
}
void DrawDlg::mouseReleaseEvent(QMouseEvent *)
{
m_moveActive = false;
}
void DrawDlg::leaveEvent(QEvent *)
{
m_moveActive = false;
}
void DrawDlg::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape)
close();
else
QWidget::keyPressEvent(event);
}
void DrawDlg::smartInvalidate()
{
QRegion region(rect());
const auto children = findChildren<QWidget *>(QString(), Qt::FindDirectChildrenOnly);
for (QWidget *child : children) {
if (!qobject_cast<Area *>(child) && child->isVisible())
region -= QRegion(child->geometry());
}
update(region);
}
void DrawDlg::centerToScreen()
{
centerToScreen(width(), height());
}
void DrawDlg::centerToScreen(int w, int h)
{
const Dpi::Monitor monitor = Dpi::Screen::primaryMonitor();
setGeometry(monitor.left() + (monitor.width() - w) / 2,
monitor.top() + (monitor.height() - h) / 2, w, h);
}
void DrawDlg::initButtons()
{
const auto children = findChildren<ButtonBase *>(QString(), Qt::FindDirectChildrenOnly);
for (ButtonBase *button : children) {
if (HGrSystem)
button->setGraphic(&HGrSystem->data);
if (auto *buttonA = qobject_cast<ButtonA *>(button))
buttonA->setFont(UniFont[static_cast<std::size_t>(FontType::Button)]);
if (auto *buttonB = qobject_cast<ButtonB *>(button))
if (HGrSystem)
buttonB->setMask(&HGrSystem->mask);
}
}
// ---- BaseMessgDlg ----
BaseMessgDlg::BaseMessgDlg(QWidget *parent) : DrawDlg(parent)
{
titleHeight = Dpi::Screen::height();
}
void BaseMessgDlg::paintEvent(QPaintEvent *)
{
QPainter painter(this);
Dpi::Canvas canvas;
canvas.bind(&painter);
paintBackground(canvas, 3 + Border, 3 + Border, width() - (6 + 2 * Border), height() - (6 + 2 * Border),
width(), height());
for (int i = 0; i <= Border; ++i)
frame(canvas, i, i, width() - 1 - i, height() - 1 - i, Qt::black, Qt::black);
if (MainTexture) {
frame(canvas, Border + 1, Border + 1, width() - (2 + Border), height() - (2 + Border),
MainTexture->colorBevelLight, MainTexture->colorBevelShade);
frame(canvas, 2 + Border, 2 + Border, width() - (3 + Border), height() - (3 + Border),
MainTexture->colorBevelLight, MainTexture->colorBevelShade);
}
splitText(false, &canvas);
if (MainTexture) {
const auto children = findChildren<ButtonBase *>(QString(), Qt::FindDirectChildrenOnly);
for (ButtonBase *button : children) {
if (button->isVisible())
btnFrame(canvas, button->geometry(), *MainTexture);
}
}
canvas.unbind();
}
void BaseMessgDlg::splitText(bool preview, Dpi::Canvas *target)
{
// Best-effort port of the 1-indexed Pascal word-wrap logic below; wraps
// messageText at word boundaries (or '\' line breaks) to fit within
// (width() - 56) px per line.
if (!MainTexture)
return;
int start = 0;
int linesCount = 0;
QPixmap dummy(1, 1);
QPainter painter(&dummy);
Dpi::Canvas canvas;
canvas.bind(&painter);
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Normal)]);
if (target)
target->font().assign(UniFont[static_cast<std::size_t>(FontType::Normal)]);
while (start < messageText.length()) {
int stop = start;
while (stop < messageText.length() - 1 && messageText.at(stop) != QLatin1Char('\\') &&
biColorTextWidth(canvas, messageText.mid(start, stop - start + 1)) < width() - 56)
++stop;
if (stop != messageText.length() - 1) {
int ordinaryStop = stop;
while (ordinaryStop > start && messageText.at(ordinaryStop) != QLatin1Char(' ') &&
messageText.at(ordinaryStop) != QLatin1Char('\\'))
--ordinaryStop;
if ((ordinaryStop - start) * 2 >= stop - start)
stop = ordinaryStop - 1;
}
if (!preview) {
const QString s = messageText.mid(start, stop - start + 1);
Dpi::Canvas &out = target ? *target : canvas;
loweredTextOut(out, -1, *MainTexture, (width() - biColorTextWidth(out, s)) / 2,
19 + Border + topSpace + linesCount * MessageLineSpacing, s);
}
start = stop + 2;
++linesCount;
}
canvas.unbind();
if (preview)
lines = linesCount;
}
void BaseMessgDlg::correctHeight()
{
const int newHeight = 72 + Border + topSpace + lines * MessageLineSpacing;
const Dpi::Monitor monitor = Dpi::Screen::primaryMonitor();
const int newTop = monitor.top() + (monitor.height() - newHeight) / 2;
setGeometry(x(), newTop, width(), newHeight);
const auto children = findChildren<QWidget *>(QString(), Qt::FindDirectChildrenOnly);
for (QWidget *child : children)
child->move(child->x(), newHeight - (34 + Border));
}
} // namespace Cevo