浏览代码

LibGUI: Add variable padding and center bitmaps in TableViews

This lets us make nicer looking bitmap tables and fixes a content
rect issue in TreeView. Also makes key column highlighting optional
thankyouverycool 4 年之前
父节点
当前提交
0fc81d23f4

+ 0 - 10
Userland/Libraries/LibGUI/AbstractTableView.cpp

@@ -392,14 +392,4 @@ void AbstractTableView::keydown_event(KeyEvent& event)
     AbstractView::keydown_event(event);
     AbstractView::keydown_event(event);
 }
 }
 
 
-int AbstractTableView::horizontal_padding() const
-{
-    return font().glyph_height() / 2;
-}
-
-int AbstractTableView::row_height() const
-{
-    return font().glyph_height() + icon_padding();
-}
-
 }
 }

+ 9 - 4
Userland/Libraries/LibGUI/AbstractTableView.h

@@ -39,7 +39,12 @@ public:
 
 
 class AbstractTableView : public AbstractView {
 class AbstractTableView : public AbstractView {
 public:
 public:
-    int row_height() const;
+    int row_height() const { return font().glyph_height() + vertical_padding(); }
+
+    virtual int horizontal_padding() const { return m_horizontal_padding; }
+    void set_horizontal_padding(int padding) { m_horizontal_padding = padding; }
+    virtual int vertical_padding() const { return m_vertical_padding; }
+    void set_vertical_padding(int padding) { m_vertical_padding = padding; }
 
 
     bool alternating_row_colors() const { return m_alternating_row_colors; }
     bool alternating_row_colors() const { return m_alternating_row_colors; }
     void set_alternating_row_colors(bool b) { m_alternating_row_colors = b; }
     void set_alternating_row_colors(bool b) { m_alternating_row_colors = b; }
@@ -59,9 +64,6 @@ public:
 
 
     void set_column_painting_delegate(int column, OwnPtr<TableCellPaintingDelegate>);
     void set_column_painting_delegate(int column, OwnPtr<TableCellPaintingDelegate>);
 
 
-    int horizontal_padding() const;
-    int icon_padding() const { return 8; }
-
     Gfx::IntPoint adjusted_position(const Gfx::IntPoint&) const;
     Gfx::IntPoint adjusted_position(const Gfx::IntPoint&) const;
 
 
     virtual Gfx::IntRect content_rect(const ModelIndex&) const override;
     virtual Gfx::IntRect content_rect(const ModelIndex&) const override;
@@ -123,6 +125,9 @@ private:
 
 
     bool m_alternating_row_colors { true };
     bool m_alternating_row_colors { true };
     bool m_highlight_selected_rows { true };
     bool m_highlight_selected_rows { true };
+
+    int m_vertical_padding { 8 };
+    int m_horizontal_padding { font().glyph_height() / 2 };
 };
 };
 
 
 }
 }

+ 11 - 2
Userland/Libraries/LibGUI/TableView.cpp

@@ -37,6 +37,8 @@
 #include <LibGUI/Window.h>
 #include <LibGUI/Window.h>
 #include <LibGfx/Palette.h>
 #include <LibGfx/Palette.h>
 
 
+REGISTER_WIDGET(GUI, TableView)
+
 namespace GUI {
 namespace GUI {
 
 
 TableView::TableView()
 TableView::TableView()
@@ -113,7 +115,7 @@ void TableView::paint_event(PaintEvent& event)
             bool is_key_column = m_key_column == column_index;
             bool is_key_column = m_key_column == column_index;
             Gfx::IntRect cell_rect(horizontal_padding() + x, y, column_width, row_height());
             Gfx::IntRect cell_rect(horizontal_padding() + x, y, column_width, row_height());
             auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
             auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
-            if (is_key_column)
+            if (is_key_column && is_key_column_highlighted())
                 painter.fill_rect(cell_rect_for_fill, key_column_background_color);
                 painter.fill_rect(cell_rect_for_fill, key_column_background_color);
             auto cell_index = model()->index(row_index, column_index);
             auto cell_index = model()->index(row_index, column_index);
 
 
@@ -122,7 +124,14 @@ void TableView::paint_event(PaintEvent& event)
             } else {
             } else {
                 auto data = cell_index.data();
                 auto data = cell_index.data();
                 if (data.is_bitmap()) {
                 if (data.is_bitmap()) {
-                    painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
+                    auto cell_constrained_bitmap_rect = data.as_bitmap().rect();
+                    if (data.as_bitmap().rect().width() > column_width)
+                        cell_constrained_bitmap_rect.set_width(column_width);
+                    if (data.as_bitmap().rect().height() > row_height())
+                        cell_constrained_bitmap_rect.set_height(row_height());
+                    cell_rect.set_y(cell_rect.y() + (row_height() - cell_constrained_bitmap_rect.height()) / 2);
+                    cell_rect.set_x(cell_rect.x() + (column_width - cell_constrained_bitmap_rect.width()) / 2);
+                    painter.blit(cell_rect.location(), data.as_bitmap(), cell_constrained_bitmap_rect);
                 } else if (data.is_icon()) {
                 } else if (data.is_icon()) {
                     if (auto bitmap = data.as_icon().bitmap_for_size(16)) {
                     if (auto bitmap = data.as_icon().bitmap_for_size(16)) {
                         cell_rect.set_y(cell_rect.y() + (row_height() - bitmap->height()) / 2);
                         cell_rect.set_y(cell_rect.y() + (row_height() - bitmap->height()) / 2);

+ 5 - 0
Userland/Libraries/LibGUI/TableView.h

@@ -51,6 +51,9 @@ public:
     GridStyle grid_style() const { return m_grid_style; }
     GridStyle grid_style() const { return m_grid_style; }
     void set_grid_style(GridStyle);
     void set_grid_style(GridStyle);
 
 
+    void set_highlight_key_column(bool b) { m_highlight_key_column = b; }
+    bool is_key_column_highlighted() const { return m_highlight_key_column; }
+
     virtual void move_cursor(CursorMovement, SelectionUpdate) override;
     virtual void move_cursor(CursorMovement, SelectionUpdate) override;
 
 
 protected:
 protected:
@@ -61,6 +64,8 @@ protected:
 
 
 private:
 private:
     GridStyle m_grid_style { GridStyle::None };
     GridStyle m_grid_style { GridStyle::None };
+
+    bool m_highlight_key_column { true };
 };
 };
 
 
 }
 }

+ 3 - 0
Userland/Libraries/LibGUI/TreeView.h

@@ -51,6 +51,8 @@ public:
     void set_should_fill_selected_rows(bool fill) { m_should_fill_selected_rows = fill; }
     void set_should_fill_selected_rows(bool fill) { m_should_fill_selected_rows = fill; }
     bool should_fill_selected_rows() const { return m_should_fill_selected_rows; }
     bool should_fill_selected_rows() const { return m_should_fill_selected_rows; }
 
 
+    virtual int vertical_padding() const override { return m_vertical_padding; }
+
 protected:
 protected:
     TreeView();
     TreeView();
 
 
@@ -89,6 +91,7 @@ private:
     RefPtr<Gfx::Bitmap> m_collapse_bitmap;
     RefPtr<Gfx::Bitmap> m_collapse_bitmap;
 
 
     bool m_should_fill_selected_rows { false };
     bool m_should_fill_selected_rows { false };
+    int m_vertical_padding { 6 };
 };
 };
 
 
 }
 }