mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 13:40:05 +00:00 
			
		
		
		
	Custom LineLabel widget to display line preview
This commit is contained in:
		
							parent
							
								
									a357489a55
								
							
						
					
					
						commit
						7254fc71fb
					
				
					 3 changed files with 221 additions and 0 deletions
				
			
		|  | @ -252,6 +252,7 @@ set(HDR_UI source/scwx/qt/ui/about_dialog.hpp | |||
|            source/scwx/qt/ui/level2_products_widget.hpp | ||||
|            source/scwx/qt/ui/level2_settings_widget.hpp | ||||
|            source/scwx/qt/ui/level3_products_widget.hpp | ||||
|            source/scwx/qt/ui/line_label.hpp | ||||
|            source/scwx/qt/ui/open_url_dialog.hpp | ||||
|            source/scwx/qt/ui/placefile_dialog.hpp | ||||
|            source/scwx/qt/ui/placefile_settings_widget.hpp | ||||
|  | @ -278,6 +279,7 @@ set(SRC_UI source/scwx/qt/ui/about_dialog.cpp | |||
|            source/scwx/qt/ui/level2_products_widget.cpp | ||||
|            source/scwx/qt/ui/level2_settings_widget.cpp | ||||
|            source/scwx/qt/ui/level3_products_widget.cpp | ||||
|            source/scwx/qt/ui/line_label.cpp | ||||
|            source/scwx/qt/ui/open_url_dialog.cpp | ||||
|            source/scwx/qt/ui/placefile_dialog.cpp | ||||
|            source/scwx/qt/ui/placefile_settings_widget.cpp | ||||
|  |  | |||
							
								
								
									
										176
									
								
								scwx-qt/source/scwx/qt/ui/line_label.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										176
									
								
								scwx-qt/source/scwx/qt/ui/line_label.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,176 @@ | |||
| #include <scwx/qt/ui/line_label.hpp> | ||||
| #include <scwx/util/logger.hpp> | ||||
| 
 | ||||
| #include <QEvent> | ||||
| #include <QPainter> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace qt | ||||
| { | ||||
| namespace ui | ||||
| { | ||||
| 
 | ||||
| static const std::string logPrefix_ = "scwx::qt::ui::line_label"; | ||||
| static const auto        logger_    = scwx::util::Logger::Create(logPrefix_); | ||||
| 
 | ||||
| class LineLabel::Impl | ||||
| { | ||||
| public: | ||||
|    explicit Impl() {}; | ||||
|    ~Impl() = default; | ||||
| 
 | ||||
|    QImage GenerateImage() const; | ||||
| 
 | ||||
|    std::size_t borderWidth_ {1}; | ||||
|    std::size_t highlightWidth_ {1}; | ||||
|    std::size_t lineWidth_ {3}; | ||||
| 
 | ||||
|    boost::gil::rgba8_pixel_t borderColor_ {0, 0, 0, 255}; | ||||
|    boost::gil::rgba8_pixel_t highlightColor_ {255, 255, 0, 255}; | ||||
|    boost::gil::rgba8_pixel_t lineColor_ {0, 0, 255, 255}; | ||||
| 
 | ||||
|    QPixmap pixmap_ {}; | ||||
|    bool    pixmapDirty_ {true}; | ||||
| }; | ||||
| 
 | ||||
| LineLabel::LineLabel(QWidget* parent) : | ||||
|     QFrame(parent), p {std::make_unique<Impl>()} | ||||
| { | ||||
| } | ||||
| 
 | ||||
| LineLabel::~LineLabel() {} | ||||
| 
 | ||||
| void LineLabel::set_border_width(std::size_t width) | ||||
| { | ||||
|    p->borderWidth_ = width; | ||||
|    p->pixmapDirty_ = true; | ||||
|    update(); | ||||
| } | ||||
| 
 | ||||
| void LineLabel::set_highlight_width(std::size_t width) | ||||
| { | ||||
|    p->highlightWidth_ = width; | ||||
|    p->pixmapDirty_    = true; | ||||
|    update(); | ||||
| } | ||||
| 
 | ||||
| void LineLabel::set_line_width(std::size_t width) | ||||
| { | ||||
|    p->lineWidth_   = width; | ||||
|    p->pixmapDirty_ = true; | ||||
|    update(); | ||||
| } | ||||
| 
 | ||||
| void LineLabel::set_border_color(boost::gil::rgba8_pixel_t color) | ||||
| { | ||||
|    p->borderColor_ = color; | ||||
|    p->pixmapDirty_ = true; | ||||
|    update(); | ||||
| } | ||||
| 
 | ||||
| void LineLabel::set_highlight_color(boost::gil::rgba8_pixel_t color) | ||||
| { | ||||
|    p->highlightColor_ = color; | ||||
|    p->pixmapDirty_    = true; | ||||
|    update(); | ||||
| } | ||||
| 
 | ||||
| void LineLabel::set_line_color(boost::gil::rgba8_pixel_t color) | ||||
| { | ||||
|    p->lineColor_   = color; | ||||
|    p->pixmapDirty_ = true; | ||||
|    update(); | ||||
| } | ||||
| 
 | ||||
| QSize LineLabel::minimumSizeHint() const | ||||
| { | ||||
|    return sizeHint(); | ||||
| } | ||||
| 
 | ||||
| QSize LineLabel::sizeHint() const | ||||
| { | ||||
|    QMargins margins = contentsMargins(); | ||||
| 
 | ||||
|    const std::size_t width = 1; | ||||
|    const std::size_t height = | ||||
|       (p->borderWidth_ + p->highlightWidth_) * 2 + p->lineWidth_; | ||||
| 
 | ||||
|    return QSize(static_cast<int>(width) + margins.left() + margins.right(), | ||||
|                 static_cast<int>(height) + margins.top() + margins.bottom()); | ||||
| } | ||||
| 
 | ||||
| void LineLabel::paintEvent(QPaintEvent* e) | ||||
| { | ||||
|    logger_->trace("paintEvent"); | ||||
| 
 | ||||
|    QFrame::paintEvent(e); | ||||
| 
 | ||||
|    if (p->pixmapDirty_) | ||||
|    { | ||||
|       QImage image    = p->GenerateImage(); | ||||
|       p->pixmap_      = QPixmap::fromImage(image); | ||||
|       p->pixmapDirty_ = false; | ||||
|    } | ||||
| 
 | ||||
|    // Don't stretch the line pixmap vertically
 | ||||
|    QRect rect = contentsRect(); | ||||
|    if (rect.height() > p->pixmap_.height()) | ||||
|    { | ||||
|       int dy  = rect.height() - p->pixmap_.height(); | ||||
|       int dy1 = dy / 2; | ||||
|       int dy2 = dy - dy1; | ||||
|       rect.adjust(0, dy1, 0, -dy2); | ||||
|    } | ||||
| 
 | ||||
|    QPainter painter(this); | ||||
|    painter.drawPixmap(rect, p->pixmap_); | ||||
| } | ||||
| 
 | ||||
| QImage LineLabel::Impl::GenerateImage() const | ||||
| { | ||||
|    const QRgb borderRgba    = qRgba(static_cast<int>(borderColor_[0]), | ||||
|                                  static_cast<int>(borderColor_[1]), | ||||
|                                  static_cast<int>(borderColor_[2]), | ||||
|                                  static_cast<int>(borderColor_[3])); | ||||
|    const QRgb highlightRgba = qRgba(static_cast<int>(highlightColor_[0]), | ||||
|                                     static_cast<int>(highlightColor_[1]), | ||||
|                                     static_cast<int>(highlightColor_[2]), | ||||
|                                     static_cast<int>(highlightColor_[3])); | ||||
|    const QRgb lineRgba      = qRgba(static_cast<int>(lineColor_[0]), | ||||
|                                static_cast<int>(lineColor_[1]), | ||||
|                                static_cast<int>(lineColor_[2]), | ||||
|                                static_cast<int>(lineColor_[3])); | ||||
| 
 | ||||
|    const std::size_t width  = 1; | ||||
|    const std::size_t height = (borderWidth_ + highlightWidth_) * 2 + lineWidth_; | ||||
| 
 | ||||
|    QImage image(static_cast<int>(width), | ||||
|                 static_cast<int>(height), | ||||
|                 QImage::Format::Format_ARGB32); | ||||
| 
 | ||||
|    std::size_t y = 0; | ||||
|    for (std::size_t i = 0; i < borderWidth_; ++i, ++y) | ||||
|    { | ||||
|       image.setPixel(0, static_cast<int>(y), borderRgba); | ||||
|       image.setPixel(0, static_cast<int>(height - 1 - y), borderRgba); | ||||
|    } | ||||
| 
 | ||||
|    for (std::size_t i = 0; i < highlightWidth_; ++i, ++y) | ||||
|    { | ||||
|       image.setPixel(0, static_cast<int>(y), highlightRgba); | ||||
|       image.setPixel(0, static_cast<int>(height - 1 - y), highlightRgba); | ||||
|    } | ||||
| 
 | ||||
|    for (std::size_t i = 0; i < lineWidth_; ++i, ++y) | ||||
|    { | ||||
|       image.setPixel(0, static_cast<int>(y), lineRgba); | ||||
|       image.setPixel(0, static_cast<int>(height - 1 - y), lineRgba); | ||||
|    } | ||||
| 
 | ||||
|    return image; | ||||
| } | ||||
| 
 | ||||
| } // namespace ui
 | ||||
| } // namespace qt
 | ||||
| } // namespace scwx
 | ||||
							
								
								
									
										43
									
								
								scwx-qt/source/scwx/qt/ui/line_label.hpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								scwx-qt/source/scwx/qt/ui/line_label.hpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,43 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <QFrame> | ||||
| 
 | ||||
| #include <boost/gil/typedefs.hpp> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace qt | ||||
| { | ||||
| namespace ui | ||||
| { | ||||
| 
 | ||||
| class LineLabel : public QFrame | ||||
| { | ||||
|    Q_OBJECT | ||||
|    Q_DISABLE_COPY_MOVE(LineLabel) | ||||
| 
 | ||||
| public: | ||||
|    explicit LineLabel(QWidget* parent = nullptr); | ||||
|    ~LineLabel(); | ||||
| 
 | ||||
|    void set_border_color(boost::gil::rgba8_pixel_t color); | ||||
|    void set_highlight_color(boost::gil::rgba8_pixel_t color); | ||||
|    void set_line_color(boost::gil::rgba8_pixel_t color); | ||||
| 
 | ||||
|    void set_border_width(std::size_t width); | ||||
|    void set_highlight_width(std::size_t width); | ||||
|    void set_line_width(std::size_t width); | ||||
| 
 | ||||
| protected: | ||||
|    QSize minimumSizeHint() const override; | ||||
|    QSize sizeHint() const override; | ||||
|    void  paintEvent(QPaintEvent* e) override; | ||||
| 
 | ||||
| private: | ||||
|    class Impl; | ||||
|    std::unique_ptr<Impl> p; | ||||
| }; | ||||
| 
 | ||||
| } // namespace ui
 | ||||
| } // namespace qt
 | ||||
| } // namespace scwx
 | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat