1+ /*
2+ * Copyright (C) by Eugen Fischer
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; either version 2 of the License, or
7+ * (at your option) any later version.
8+ *
9+ * This program is distributed in the hope that it will be useful, but
10+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+ * for more details.
13+ */
14+
15+ #include " nmcadvertwidget.h"
16+ #include < QDebug>
17+ #include < QBoxLayout>
18+ #include < QIcon>
19+ #include < QCoreApplication>
20+ #include < QGraphicsView>
21+ #include < QMouseEvent>
22+ #include < QDebug>
23+
24+ NMCAdvertWidget::NMCAdvertWidget (QWidget *parent)
25+ : QWidget(parent),
26+ m_graphicsView(new NMCCustomGraphicsView(this ))
27+ {
28+ setFixedSize (698 , 474 );
29+ auto *layout = new QHBoxLayout (this );
30+ setLayout (layout);
31+
32+ m_graphicsView->setScene (&m_graphicsScene);
33+ layout->addWidget (m_graphicsView);
34+ layout->setContentsMargins (0 , 0 , 0 , 0 );
35+
36+ m_graphicsView->setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
37+ m_graphicsView->setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
38+
39+ generatePixmapList (QStringLiteral (" :/client/theme/NMCIcons/configuration1.png" ));
40+ generatePixmapList (QStringLiteral (" :/client/theme/NMCIcons/configuration2.png" ));
41+ generatePixmapList (QStringLiteral (" :/client/theme/NMCIcons/configuration3.png" ));
42+
43+ initStartButton ();
44+
45+ m_arrow_left = new NMCClickableLabel (m_graphicsView);
46+ m_arrow_left->setPixmap (QIcon (QStringLiteral (" :/client/theme/NMCIcons/navigation-left.svg" )).pixmap (32 , 32 ));
47+ connect (m_arrow_left, &NMCClickableLabel::clicked, this , &NMCAdvertWidget::onArrowLeftClicked);
48+
49+ m_arrow_right = new NMCClickableLabel (m_graphicsView);
50+ m_arrow_right->setPixmap (QIcon (QStringLiteral (" :/client/theme/NMCIcons/navigation-right.svg" )).pixmap (32 , 32 ));
51+ connect (m_arrow_right, &NMCClickableLabel::clicked, this , &NMCAdvertWidget::onArrowRightClicked);
52+
53+ if (!m_pixmapList.empty ()) {
54+ loadPNG (m_pixmapList.first ());
55+ m_currentImageId = 0 ;
56+ }
57+
58+ m_animationTimer.setInterval (5000 );
59+ connect (&m_animationTimer, &QTimer::timeout, this , [this ]() {
60+ ++m_currentImageId;
61+ if (m_currentImageId >= m_pixmapList.size ()) {
62+ m_currentImageId = 0 ;
63+ }
64+ selectTextByID ();
65+ });
66+
67+ m_animationTimer.start ();
68+ setStartButton ();
69+ setDetailText (QCoreApplication::translate (" " , " ADVERT_DETAIL_TEXT_1" ));
70+ setHeaderText (QCoreApplication::translate (" " , " ADVERT_HEADER_TEXT_1" ));
71+ setHeader (QCoreApplication::translate (" " , " ADVERT_HEADER_1" ));
72+ setArrows ();
73+ }
74+
75+ void NMCAdvertWidget::resizeEvent (QResizeEvent *event)
76+ {
77+ QWidget::resizeEvent (event);
78+ if (m_currentImageId < m_pixmapList.size ())
79+ loadPNG (m_pixmapList.at (m_currentImageId));
80+
81+ setStartButton ();
82+ setDetailText (m_detailText ? m_detailText->text () : QString ());
83+ setHeaderText (m_headerText ? m_headerText->text () : QString ());
84+ setHeader (m_header ? m_header->text () : QString ());
85+ setArrows ();
86+ }
87+
88+ void NMCAdvertWidget::clearScene ()
89+ {
90+ m_graphicsScene.clear ();
91+ }
92+
93+ void NMCAdvertWidget::loadPNG (const QPixmap &pixmap)
94+ {
95+ clearScene ();
96+ auto *pixmapItem = m_graphicsScene.addPixmap (pixmap.scaled (size (), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
97+ if (pixmapItem) {
98+ m_graphicsView->setFixedSize (pixmapItem->pixmap ().size ());
99+ m_graphicsView->update ();
100+ }
101+ }
102+
103+ void NMCAdvertWidget::generatePixmapList (const QString &name)
104+ {
105+ QPixmap pixmap (name);
106+ m_pixmapList.append (pixmap.scaled (size (), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
107+ }
108+
109+ void NMCAdvertWidget::initStartButton ()
110+ {
111+ if (!m_pushButton) {
112+ m_pushButton = new QPushButton (QCoreApplication::translate (" " , " START_NOW" ), m_graphicsView);
113+ m_pushButton->setStyleSheet (QStringLiteral (
114+ " QPushButton { font-size: 15px; border: 0px solid black; border-radius: 4px; background-color: white; color: black; }"
115+ " QPushButton:hover { background-color: #ededed; }"
116+ ));
117+ // m_pushButton->setFixedSize(kButtonWidth, kButtonHeight);
118+ m_pushButton->setFixedSize (140 , 36 );
119+ connect (m_pushButton, &QPushButton::clicked, this , &NMCAdvertWidget::close);
120+ }
121+ }
122+
123+ void NMCAdvertWidget::setStartButton ()
124+ {
125+ if (m_pushButton && m_graphicsView) {
126+ m_graphicsScene.addWidget (m_pushButton);
127+ int buttonWidth = 140 ;
128+ int buttonHeight = 36 ;
129+ int x = (width () - buttonWidth) / 2 ;
130+ int y = height () - 60 ;
131+ m_pushButton->setGeometry (x, y, buttonWidth, buttonHeight);
132+ }
133+ }
134+
135+ void NMCAdvertWidget::setDetailText (const QString &p_text)
136+ {
137+ if (!m_detailText) {
138+ m_detailText = new QLabel (p_text, m_graphicsView);
139+ m_detailText->setWordWrap (true );
140+ m_detailText->setAlignment (Qt::AlignCenter);
141+ m_detailText->setStyleSheet (QStringLiteral (" font-size: 15px; color: white" ));
142+ m_graphicsScene.addWidget (m_detailText);
143+ } else {
144+ m_detailText->setText (p_text);
145+ }
146+
147+ m_detailText->setFixedWidth (420 );
148+
149+ int y = height () - 88 - m_detailText->sizeHint ().height ();
150+ int x = width () / 2 - m_detailText->sizeHint ().width () / 2 ;
151+
152+ m_detailText->setGeometry (x, y, m_detailText->sizeHint ().width (), m_detailText->sizeHint ().height ());
153+ }
154+
155+ void NMCAdvertWidget::setHeaderText (const QString &p_text)
156+ {
157+ if (!m_headerText) {
158+ m_headerText = new QLabel (p_text, m_graphicsView);
159+ m_headerText->setWordWrap (true );
160+ m_headerText->setAlignment (Qt::AlignCenter);
161+ m_headerText->setStyleSheet (QStringLiteral (" font-size: 26px; color: white" ));
162+ m_graphicsScene.addWidget (m_headerText);
163+ } else {
164+ m_headerText->setText (p_text);
165+ }
166+
167+ m_headerText->setFixedWidth (420 );
168+
169+ int y = height () - 100 - m_detailText->sizeHint ().height () - m_headerText->sizeHint ().height ();
170+ int x = width () / 2 - m_headerText->sizeHint ().width () / 2 ;
171+
172+ m_headerText->setGeometry (x, y, m_headerText->sizeHint ().width (), m_headerText->sizeHint ().height ());
173+ }
174+
175+ void NMCAdvertWidget::setHeader (const QString &p_text)
176+ {
177+ if (!m_header) {
178+ m_header = new QLabel (p_text, m_graphicsView);
179+ m_header->setAlignment (Qt::AlignCenter);
180+ m_header->setStyleSheet (QStringLiteral (" font-size: 22px; color: white; font-weight: bold;" ));
181+ m_graphicsScene.addWidget (m_header);
182+ } else {
183+ m_header->setText (p_text);
184+ }
185+
186+ int y = height () - 142 - m_detailText->sizeHint ().height () - m_headerText->sizeHint ().height ();
187+ int x = width () / 2 - m_header->sizeHint ().width () / 2 ;
188+
189+ m_header->setGeometry (x, y, m_header->sizeHint ().width (), m_header->sizeHint ().height ());
190+ }
191+
192+ void NMCAdvertWidget::setArrows ()
193+ {
194+ int y = height () - kArrowOffset ;
195+ m_arrow_left->move (qMax (112 , 20 ), y);
196+ m_arrow_right->move (qMin (width () - 130 , width () - 50 ), y);
197+ }
198+
199+ void NMCAdvertWidget::loadPicture (bool next)
200+ {
201+ m_currentImageId = (m_currentImageId + (next ? 1 : -1 ) + 3 ) % 3 ;
202+ selectTextByID ();
203+ }
204+
205+ void NMCAdvertWidget::selectTextByID ()
206+ {
207+ loadPNG (m_pixmapList.at (m_currentImageId));
208+
209+ switch (m_currentImageId) {
210+ case 0 :
211+ setDetailText (QCoreApplication::translate (" " , " ADVERT_DETAIL_TEXT_1" ));
212+ setHeaderText (QCoreApplication::translate (" " , " ADVERT_HEADER_TEXT_1" ));
213+ break ;
214+ case 1 :
215+ setDetailText (QCoreApplication::translate (" " , " ADVERT_DETAIL_TEXT_2" ));
216+ setHeaderText (QCoreApplication::translate (" " , " ADVERT_HEADER_TEXT_2" ));
217+ break ;
218+ case 2 :
219+ setDetailText (QCoreApplication::translate (" " , " ADVERT_DETAIL_TEXT_3" ));
220+ setHeaderText (QCoreApplication::translate (" " , " ADVERT_HEADER_TEXT_3" ));
221+ break ;
222+ default :
223+ break ;
224+ }
225+
226+ if (m_header) {
227+ m_header->setVisible (m_currentImageId == 0 );
228+ }
229+ }
230+
231+ void NMCAdvertWidget::onArrowLeftClicked ()
232+ {
233+ m_animationTimer.stop ();
234+ loadPicture (false );
235+ }
236+
237+ void NMCAdvertWidget::onArrowRightClicked ()
238+ {
239+ m_animationTimer.stop ();
240+ loadPicture (true );
241+ }
242+
243+ // Definition von NMCCustomGraphicsView
244+ NMCCustomGraphicsView::NMCCustomGraphicsView (QWidget *parent)
245+ : QGraphicsView(parent)
246+ {
247+ setStyleSheet (QStringLiteral (" background: transparent" ));
248+ setFrameStyle (QFrame::NoFrame);
249+ }
250+
251+ void NMCCustomGraphicsView::wheelEvent (QWheelEvent *event)
252+ {
253+ // Unterdrücke Scrollen
254+ event->ignore ();
255+ }
256+
257+ // Definition von NMCClickableLabel
258+ NMCClickableLabel::NMCClickableLabel (QWidget *parent)
259+ : QLabel(parent)
260+ {
261+ setCursor (Qt::PointingHandCursor);
262+ }
263+
264+ void NMCClickableLabel::mousePressEvent (QMouseEvent *event)
265+ {
266+ if (event->button () == Qt::LeftButton) {
267+ emit clicked ();
268+ }
269+ QLabel::mousePressEvent (event);
270+ }
0 commit comments