mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 18:57:42 +00:00
LibGUI: Add a setting to make GLabel stretch its icon.
Use this in QuickShow to allow arbitrarily scaling the opened image.
This commit is contained in:
parent
86570d3b1a
commit
d563dc0565
3 changed files with 38 additions and 4 deletions
|
@ -34,11 +34,16 @@ void GLabel::paint_event(GPaintEvent& event)
|
||||||
{
|
{
|
||||||
Painter painter(*this);
|
Painter painter(*this);
|
||||||
painter.set_clip_rect(event.rect());
|
painter.set_clip_rect(event.rect());
|
||||||
|
|
||||||
if (fill_with_background_color())
|
if (fill_with_background_color())
|
||||||
painter.fill_rect({ 0, 0, width(), height() }, background_color());
|
painter.fill_rect({ 0, 0, width(), height() }, background_color());
|
||||||
if (m_icon) {
|
if (m_icon) {
|
||||||
auto icon_location = rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
|
if (m_should_stretch_icon) {
|
||||||
painter.blit(icon_location, *m_icon, m_icon->rect());
|
painter.draw_scaled_bitmap(rect(), *m_icon, m_icon->rect());
|
||||||
|
} else {
|
||||||
|
auto icon_location = rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
|
||||||
|
painter.blit(icon_location, *m_icon, m_icon->rect());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!text().is_empty())
|
if (!text().is_empty())
|
||||||
painter.draw_text({ 0, 0, width(), height() }, text(), m_text_alignment, foreground_color());
|
painter.draw_text({ 0, 0, width(), height() }, text(), m_text_alignment, foreground_color());
|
||||||
|
|
|
@ -22,6 +22,9 @@ public:
|
||||||
TextAlignment text_alignment() const { return m_text_alignment; }
|
TextAlignment text_alignment() const { return m_text_alignment; }
|
||||||
void set_text_alignment(TextAlignment text_alignment) { m_text_alignment = text_alignment; }
|
void set_text_alignment(TextAlignment text_alignment) { m_text_alignment = text_alignment; }
|
||||||
|
|
||||||
|
bool should_stretch_icon() const { return m_should_stretch_icon; }
|
||||||
|
void set_should_stretch_icon(bool b) { m_should_stretch_icon = b; }
|
||||||
|
|
||||||
void size_to_fit();
|
void size_to_fit();
|
||||||
|
|
||||||
virtual const char* class_name() const override { return "GLabel"; }
|
virtual const char* class_name() const override { return "GLabel"; }
|
||||||
|
@ -32,5 +35,6 @@ private:
|
||||||
String m_text;
|
String m_text;
|
||||||
RetainPtr<GraphicsBitmap> m_icon;
|
RetainPtr<GraphicsBitmap> m_icon;
|
||||||
TextAlignment m_text_alignment { TextAlignment::Center };
|
TextAlignment m_text_alignment { TextAlignment::Center };
|
||||||
|
bool m_should_stretch_icon { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -2,12 +2,36 @@
|
||||||
#include <LibGUI/GApplication.h>
|
#include <LibGUI/GApplication.h>
|
||||||
#include <LibGUI/GWindow.h>
|
#include <LibGUI/GWindow.h>
|
||||||
#include <LibGUI/GLabel.h>
|
#include <LibGUI/GLabel.h>
|
||||||
|
#include <LibGUI/GBoxLayout.h>
|
||||||
|
#include <LibGUI/GMenuBar.h>
|
||||||
|
#include <LibGUI/GMenu.h>
|
||||||
|
#include <LibGUI/GAction.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
GApplication app(argc, argv);
|
GApplication app(argc, argv);
|
||||||
|
|
||||||
|
auto menubar = make<GMenuBar>();
|
||||||
|
|
||||||
|
auto app_menu = make<GMenu>("QuickShow");
|
||||||
|
app_menu->add_action(GAction::create("Quit", { Mod_Alt, Key_F4 }, [] (const GAction&) {
|
||||||
|
GApplication::the().quit(0);
|
||||||
|
return;
|
||||||
|
}));
|
||||||
|
menubar->add_menu(move(app_menu));
|
||||||
|
|
||||||
|
auto file_menu = make<GMenu>("File");
|
||||||
|
menubar->add_menu(move(file_menu));
|
||||||
|
|
||||||
|
auto help_menu = make<GMenu>("Help");
|
||||||
|
help_menu->add_action(GAction::create("About", [] (const GAction&) {
|
||||||
|
dbgprintf("FIXME: Implement Help/About\n");
|
||||||
|
}));
|
||||||
|
menubar->add_menu(move(help_menu));
|
||||||
|
|
||||||
|
app.set_menubar(move(menubar));
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
printf("usage: qs <image-file>\n");
|
printf("usage: qs <image-file>\n");
|
||||||
|
@ -32,12 +56,13 @@ int main(int argc, char** argv)
|
||||||
window->set_rect(200, 200, bitmap->width(), bitmap->height());
|
window->set_rect(200, 200, bitmap->width(), bitmap->height());
|
||||||
|
|
||||||
auto* widget = new GWidget;
|
auto* widget = new GWidget;
|
||||||
widget->set_fill_with_background_color(true);
|
|
||||||
window->set_main_widget(widget);
|
window->set_main_widget(widget);
|
||||||
|
|
||||||
|
widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
|
||||||
|
|
||||||
auto* label = new GLabel(widget);
|
auto* label = new GLabel(widget);
|
||||||
label->set_relative_rect({ 0, 0, bitmap->width(), bitmap->height() });
|
|
||||||
label->set_icon(move(bitmap));
|
label->set_icon(move(bitmap));
|
||||||
|
label->set_should_stretch_icon(true);
|
||||||
|
|
||||||
window->set_should_exit_event_loop_on_close(true);
|
window->set_should_exit_event_loop_on_close(true);
|
||||||
window->show();
|
window->show();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue