From 4c0b1f31fedfbdcd89c4331ec39aa1a9e52b8eba Mon Sep 17 00:00:00 2001 From: Raphael Robatsch Date: Fri, 22 Oct 2021 15:34:19 +0200 Subject: [PATCH] Draw tags --- meson.build | 3 +-- src/bar.cpp | 44 +++++++++++++++++++++++++++++++++++++++----- src/bar.hpp | 15 +++++++++++++-- src/bar_widget.cpp | 18 ------------------ src/bar_widget.hpp | 17 ----------------- src/config.hpp | 4 +++- src/main.cpp | 4 ++-- 7 files changed, 58 insertions(+), 47 deletions(-) delete mode 100644 src/bar_widget.cpp delete mode 100644 src/bar_widget.hpp diff --git a/meson.build b/meson.build index 4483abc..8efeca4 100644 --- a/meson.build +++ b/meson.build @@ -3,7 +3,7 @@ project('somebar', ['c', 'cpp'], wayland_dep = dependency('wayland-client') qt5 = import('qt5') -qt5_dep = dependency('qt5', modules: ['Core', 'Widgets']) +qt5_dep = dependency('qt5', modules: ['Core', 'Gui']) #moc = qt5.compile_moc(headers: [ 'src/.hpp', ]) subdir('protocols') @@ -12,7 +12,6 @@ executable('somebar', 'src/main.cpp', 'src/shm_buffer.cpp', 'src/bar.cpp', - 'src/bar_widget.cpp', wayland_sources, #moc, dependencies: [qt5_dep, wayland_dep]) diff --git a/src/bar.cpp b/src/bar.cpp index 15ae220..30e8624 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -1,7 +1,9 @@ // somebar - dwl bar // See LICENSE file for copyright and license details. +#include #include +#include #include "bar.hpp" #include "config.hpp" @@ -24,6 +26,10 @@ Bar::Bar(const wl_output *output) zwlr_layer_surface_v1_set_size(_layerSurface, 0, barSize); zwlr_layer_surface_v1_set_exclusive_zone(_layerSurface, barSize); wl_surface_commit(_surface); + + for (auto i=1; i<=4; i++) { + _tags.push_back({ QString::number(i), i%2 == 0 }); + } } Bar::~Bar() @@ -36,10 +42,11 @@ void Bar::layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height { zwlr_layer_surface_v1_ack_configure(_layerSurface, serial); _bufs.emplace(width, height, WL_SHM_FORMAT_XRGB8888); - auto root = _widget.root(); - root->setFixedSize(width, height); render(); } +static QBrush inactiveBg = {QColor::fromRgb(0, 0, 0)}; +static QBrush activeBg = QBrush {QColor::fromRgb(0, 0, 255)}; +static QPen fg = QPen {QBrush {QColor::fromRgb(255, 255, 255)}, 1}; void Bar::render() { @@ -48,11 +55,38 @@ void Bar::render() _bufs->width, _bufs->height, _bufs->stride, - QImage::Format_RGBX8888 + QImage::Format_ARGB32 }; - auto root = _widget.root(); - root->render(&img); + auto painter = QPainter {&img}; + auto font = painter.font(); + font.setBold(true); + font.setPixelSize(18); + painter.setFont(font); + painter.setPen(fg); + + painter.fillRect(0, 0, img.width(), img.height(), activeBg); + _fontMetrics.emplace(painter.font()); + _textY = _fontMetrics->ascent() + paddingY; + renderTags(painter); + wl_surface_attach(_surface, _bufs->buffer(), 0, 0); wl_surface_commit(_surface); _bufs->flip(); } + +void Bar::renderTags(QPainter &painter) +{ + auto x = 0; + for (const auto &tag : _tags) { + auto size = textWidth(tag.name) + paddingX*2; + auto& bg = tag.active ? activeBg : inactiveBg; + painter.fillRect(x, 0, size, barSize, bg); + painter.drawText(paddingX+x, _textY, tag.name); + x += size; + } +} + +int Bar::textWidth(const QString &text) +{ + return _fontMetrics->size(Qt::TextSingleLine, text).width(); +} diff --git a/src/bar.hpp b/src/bar.hpp index e79f975..bbc97ee 100644 --- a/src/bar.hpp +++ b/src/bar.hpp @@ -3,22 +3,33 @@ #pragma once #include +#include #include +#include +#include #include "wlr-layer-shell-unstable-v1-client-protocol.h" #include "common.hpp" #include "shm_buffer.hpp" -#include "bar_widget.hpp" + +struct Tag { + QString name; + bool active; +}; class Bar { static const zwlr_layer_surface_v1_listener _layerSurfaceListener; wl_surface *_surface {nullptr}; zwlr_layer_surface_v1 *_layerSurface {nullptr}; + std::optional _fontMetrics; std::optional _bufs; - BarWidget _widget; + std::vector _tags; + int _textY; void layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height); void render(); + void renderTags(QPainter &painter); + int textWidth(const QString &text); public: explicit Bar(const wl_output *output); ~Bar(); diff --git a/src/bar_widget.cpp b/src/bar_widget.cpp deleted file mode 100644 index 42e6597..0000000 --- a/src/bar_widget.cpp +++ /dev/null @@ -1,18 +0,0 @@ -// somebar - dwl bar -// See LICENSE file for copyright and license details. - -#include "bar_widget.hpp" - -BarWidget::BarWidget() - : _box {QBoxLayout::LeftToRight, &_root} - , _label {&_root} -{ - _root.setAttribute(Qt::WA_DontShowOnScreen); - _label.setText("somebar"); -} - -BarWidget::~BarWidget() -{ -} - -QWidget* BarWidget::root() { return &_root; } diff --git a/src/bar_widget.hpp b/src/bar_widget.hpp deleted file mode 100644 index e013714..0000000 --- a/src/bar_widget.hpp +++ /dev/null @@ -1,17 +0,0 @@ -// somebar - dwl bar -// See LICENSE file for copyright and license details. - -#pragma once -#include -#include -#include - -class BarWidget { - QWidget _root; - QBoxLayout _box; - QLabel _label; -public: - explicit BarWidget(); - ~BarWidget(); - QWidget* root(); -}; diff --git a/src/config.hpp b/src/config.hpp index 4759a5c..9e3df03 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -1,5 +1,7 @@ // somebar - dwl bar // See LICENSE file for copyright and license details. -constexpr int barSize = 20; +constexpr int barSize = 26; constexpr bool topbar = 1; +constexpr int paddingX = 10; +constexpr int paddingY = 3; diff --git a/src/main.cpp b/src/main.cpp index 1a9537f..29bf871 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include #include #include "qnamespace.h" @@ -68,7 +68,7 @@ static const struct wl_registry_listener registry_listener = { registryHandleGlo int main(int argc, char **argv) { - QApplication app(argc, argv); + QGuiApplication app(argc, argv); QCoreApplication::setOrganizationName("tape software"); QCoreApplication::setOrganizationDomain("tapesoftware.net"); QCoreApplication::setApplicationName("somebar");