├── README.md ├── frmmain.cpp ├── frmmain.h ├── frmmain.ui ├── images ├── Thumbs.db ├── next.png └── prev.png ├── main.cpp ├── myResource.qrc ├── myValueControl.pro ├── myaroundcircle.cpp ├── myaroundcircle.h ├── mybattery.cpp ├── mybattery.h ├── mybuttonboard.cpp ├── mybuttonboard.h ├── mychart1.cpp ├── mychart1.h ├── mycoolbar.cpp ├── mycoolbar.h ├── myflashlist.cpp ├── myflashlist.h ├── mygauge1.cpp ├── mygauge1.h ├── mygauge2.cpp ├── mygauge2.h ├── mygauge3.cpp ├── mygauge3.h ├── mygauge4.cpp ├── mygauge4.h ├── mygauge5.cpp ├── mygauge5.h ├── mygauge6.cpp ├── mygauge6.h ├── mygauge7.cpp ├── mygauge7.h ├── myindicator.cpp ├── myindicator.h ├── mylcdnumber.cpp ├── mylcdnumber.h ├── mynewled.cpp ├── mynewled.h ├── myphotoview.cpp ├── myphotoview.h ├── myprogressbar.cpp ├── myprogressbar.h ├── myprogressbar1.cpp ├── myprogressbar1.h ├── mysliderbutton.cpp ├── mysliderbutton.h ├── myvolume.cpp ├── myvolume.h ├── mywave.cpp ├── mywave.h ├── mywave1.cpp ├── mywave1.h ├── mywave2.cpp └── mywave2.h /README.md: -------------------------------------------------------------------------------- 1 | # QtControls 2 | 视频演示: 3 | [点击观看演示视频](https://www.bilibili.com/video/bv1u7411976V) 4 | 关注微信公众号:嵌入式基地 5 | 后台回复:嵌入式基地 获取更多资料 6 | 7 | -------------------------------------------------------------------------------- /frmmain.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lighter-z/QtControls/d7cf6b4b5dad4cae8b8999438178b7c5d6ddc1ba/frmmain.cpp -------------------------------------------------------------------------------- /frmmain.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lighter-z/QtControls/d7cf6b4b5dad4cae8b8999438178b7c5d6ddc1ba/frmmain.h -------------------------------------------------------------------------------- /frmmain.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | frmMain 4 | 5 | 6 | 7 | 0 8 | 0 9 | 642 10 | 548 11 | 12 | 13 | 14 | 控件演示 15 | 16 | 17 | 18 | 19 | 10 20 | 10 21 | 625 22 | 533 23 | 24 | 25 | 26 | 1 27 | 28 | 29 | true 30 | 31 | 32 | false 33 | 34 | 35 | 36 | 各种各样的控件 37 | 38 | 39 | 40 | 41 | 0 42 | 0 43 | 481 44 | 509 45 | 46 | 47 | 48 | QFrame::StyledPanel 49 | 50 | 51 | QFrame::Raised 52 | 53 | 54 | 55 | 56 | 57 | 490 58 | 480 59 | 131 60 | 28 61 | 62 | 63 | 64 | 65 | 66 | 67 | 当前值: 68 | 69 | 70 | 71 | 72 | 73 | 490 74 | 120 75 | 125 76 | 29 77 | 78 | 79 | 80 | PointingHandCursor 81 | 82 | 83 | 线性刻度指示仪 84 | 85 | 86 | 87 | 88 | 89 | 490 90 | 200 91 | 125 92 | 29 93 | 94 | 95 | 96 | PointingHandCursor 97 | 98 | 99 | 垂直右刻度指示仪 100 | 101 | 102 | 103 | 104 | 105 | 490 106 | 280 107 | 125 108 | 29 109 | 110 | 111 | 112 | PointingHandCursor 113 | 114 | 115 | 酷炫进度条 116 | 117 | 118 | 119 | 120 | 121 | 490 122 | 440 123 | 125 124 | 29 125 | 126 | 127 | 128 | PointingHandCursor 129 | 130 | 131 | 频谱分析控件 132 | 133 | 134 | 135 | 136 | 137 | 490 138 | 160 139 | 125 140 | 29 141 | 142 | 143 | 144 | PointingHandCursor 145 | 146 | 147 | 圆形刻度指示仪 148 | 149 | 150 | 151 | 152 | 153 | 490 154 | 40 155 | 125 156 | 29 157 | 158 | 159 | 160 | PointingHandCursor 161 | 162 | 163 | 圆形指针指示仪 164 | 165 | 166 | 167 | 168 | 169 | 490 170 | 80 171 | 125 172 | 29 173 | 174 | 175 | 176 | PointingHandCursor 177 | 178 | 179 | 左右滑块指示仪 180 | 181 | 182 | 183 | 184 | 185 | 490 186 | 360 187 | 125 188 | 29 189 | 190 | 191 | 192 | PointingHandCursor 193 | 194 | 195 | 椭圆滑块 196 | 197 | 198 | 199 | 200 | 201 | 490 202 | 3 203 | 125 204 | 29 205 | 206 | 207 | 208 | PointingHandCursor 209 | 210 | 211 | 圆形拱形指示仪 212 | 213 | 214 | 215 | 216 | 217 | 490 218 | 400 219 | 125 220 | 29 221 | 222 | 223 | 224 | PointingHandCursor 225 | 226 | 227 | 垂直方块进度条 228 | 229 | 230 | 231 | 232 | 233 | 490 234 | 240 235 | 125 236 | 29 237 | 238 | 239 | 240 | PointingHandCursor 241 | 242 | 243 | 垂直左刻度指示仪 244 | 245 | 246 | 247 | 248 | 249 | 490 250 | 320 251 | 125 252 | 29 253 | 254 | 255 | 256 | PointingHandCursor 257 | 258 | 259 | 电源指示仪 260 | 261 | 262 | 263 | 264 | 265 | 各式各样的控件 266 | 267 | 268 | 269 | 270 | 0 271 | 0 272 | 481 273 | 509 274 | 275 | 276 | 277 | QFrame::StyledPanel 278 | 279 | 280 | QFrame::Raised 281 | 282 | 283 | 284 | 285 | 286 | 490 287 | 3 288 | 125 289 | 29 290 | 291 | 292 | 293 | PointingHandCursor 294 | 295 | 296 | 图表曲线显示 297 | 298 | 299 | 300 | 301 | 302 | 490 303 | 480 304 | 131 305 | 28 306 | 307 | 308 | 309 | 310 | 311 | 312 | 当前值: 313 | 314 | 315 | 316 | 317 | 318 | 490 319 | 40 320 | 125 321 | 29 322 | 323 | 324 | 325 | PointingHandCursor 326 | 327 | 328 | 滑动列表框 329 | 330 | 331 | 332 | 333 | 334 | 490 335 | 80 336 | 125 337 | 29 338 | 339 | 340 | 341 | PointingHandCursor 342 | 343 | 344 | 按钮面板 345 | 346 | 347 | 348 | 349 | 350 | 490 351 | 120 352 | 125 353 | 29 354 | 355 | 356 | 357 | PointingHandCursor 358 | 359 | 360 | 滑动背景面板 361 | 362 | 363 | 364 | 365 | 366 | 490 367 | 160 368 | 125 369 | 29 370 | 371 | 372 | 373 | PointingHandCursor 374 | 375 | 376 | 图片浏览工具 377 | 378 | 379 | 380 | 381 | 382 | 490 383 | 200 384 | 125 385 | 29 386 | 387 | 388 | 389 | PointingHandCursor 390 | 391 | 392 | 旋转的圆 393 | 394 | 395 | 396 | 397 | 398 | 490 399 | 240 400 | 125 401 | 29 402 | 403 | 404 | 405 | PointingHandCursor 406 | 407 | 408 | 另类进度条 409 | 410 | 411 | 412 | 413 | 414 | 490 415 | 280 416 | 125 417 | 29 418 | 419 | 420 | 421 | PointingHandCursor 422 | 423 | 424 | LED数字显示 425 | 426 | 427 | 428 | 429 | 430 | 490 431 | 320 432 | 125 433 | 29 434 | 435 | 436 | 437 | PointingHandCursor 438 | 439 | 440 | 声音波形动态图 441 | 442 | 443 | 444 | 445 | 446 | 490 447 | 360 448 | 125 449 | 29 450 | 451 | 452 | 453 | PointingHandCursor 454 | 455 | 456 | 声音直方动态图 457 | 458 | 459 | 460 | 461 | 462 | 490 463 | 400 464 | 125 465 | 29 466 | 467 | 468 | 469 | PointingHandCursor 470 | 471 | 472 | 对称直方动态图 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | -------------------------------------------------------------------------------- /images/Thumbs.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lighter-z/QtControls/d7cf6b4b5dad4cae8b8999438178b7c5d6ddc1ba/images/Thumbs.db -------------------------------------------------------------------------------- /images/next.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lighter-z/QtControls/d7cf6b4b5dad4cae8b8999438178b7c5d6ddc1ba/images/next.png -------------------------------------------------------------------------------- /images/prev.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lighter-z/QtControls/d7cf6b4b5dad4cae8b8999438178b7c5d6ddc1ba/images/prev.png -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lighter-z/QtControls/d7cf6b4b5dad4cae8b8999438178b7c5d6ddc1ba/main.cpp -------------------------------------------------------------------------------- /myResource.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | images/next.png 4 | images/prev.png 5 | 6 | 7 | -------------------------------------------------------------------------------- /myValueControl.pro: -------------------------------------------------------------------------------- 1 | 2 | QT += core gui widgets 3 | 4 | TARGET = myValueControl 5 | TEMPLATE = app 6 | 7 | 8 | SOURCES += main.cpp\ 9 | frmmain.cpp \ 10 | mygauge1.cpp \ 11 | mygauge2.cpp \ 12 | mygauge3.cpp \ 13 | mygauge4.cpp \ 14 | mygauge5.cpp \ 15 | mygauge6.cpp \ 16 | myprogressbar.cpp \ 17 | mygauge7.cpp \ 18 | mybattery.cpp \ 19 | mycoolbar.cpp \ 20 | mylcdnumber.cpp \ 21 | myindicator.cpp \ 22 | myvolume.cpp \ 23 | mychart1.cpp \ 24 | myflashlist.cpp \ 25 | mybuttonboard.cpp \ 26 | mysliderbutton.cpp \ 27 | myphotoview.cpp \ 28 | myaroundcircle.cpp \ 29 | myprogressbar1.cpp \ 30 | mynewled.cpp \ 31 | mywave.cpp \ 32 | mywave1.cpp \ 33 | mywave2.cpp 34 | 35 | HEADERS += frmmain.h \ 36 | mygauge1.h \ 37 | mygauge2.h \ 38 | mygauge3.h \ 39 | mygauge4.h \ 40 | mygauge5.h \ 41 | mygauge6.h \ 42 | myprogressbar.h \ 43 | mygauge7.h \ 44 | mybattery.h \ 45 | mycoolbar.h \ 46 | mylcdnumber.h \ 47 | myindicator.h \ 48 | myvolume.h \ 49 | mychart1.h \ 50 | myflashlist.h \ 51 | mybuttonboard.h \ 52 | mysliderbutton.h \ 53 | myphotoview.h \ 54 | myaroundcircle.h \ 55 | myprogressbar1.h \ 56 | mynewled.h \ 57 | mywave.h \ 58 | mywave1.h \ 59 | mywave2.h 60 | 61 | FORMS += frmmain.ui 62 | 63 | RESOURCES += \ 64 | myResource.qrc 65 | -------------------------------------------------------------------------------- /myaroundcircle.cpp: -------------------------------------------------------------------------------- 1 | #include "myaroundcircle.h" 2 | 3 | myAroundCircle::myAroundCircle(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | m_updateTimer = new QTimer(this); 7 | m_updateTimer->setInterval(10); 8 | connect(m_updateTimer,SIGNAL(timeout()),this,SLOT(UpdateAngle())); 9 | m_updateTimer->start(); 10 | 11 | m_angle = 0; 12 | m_outerRadius = 0; 13 | setWindowFlags(Qt::FramelessWindowHint); 14 | setAttribute(Qt::WA_TranslucentBackground); 15 | } 16 | 17 | void myAroundCircle::paintEvent(QPaintEvent *) 18 | { 19 | QPainter painter(this); 20 | painter.setRenderHints(QPainter::Antialiasing|QPainter::HighQualityAntialiasing); 21 | drawUnderCircle(&painter); 22 | drawBMW(&painter); 23 | } 24 | 25 | void myAroundCircle::drawUnderCircle(QPainter *painter) 26 | { 27 | painter->save(); 28 | 29 | m_outerRadius = width() > height() ? (qreal)height()/2 : (qreal)width()/2; 30 | 31 | QPointF TopLeft(rect().center().x() - m_outerRadius,rect().center().y() - m_outerRadius); 32 | QPointF BottomRight(rect().center().x() + m_outerRadius,rect().center().y() + m_outerRadius); 33 | QRectF CircleRect(TopLeft,BottomRight); 34 | 35 | painter->setPen(Qt::NoPen); 36 | 37 | QRadialGradient CircleGradient(CircleRect.center(),m_outerRadius,CircleRect.center()); 38 | CircleGradient.setColorAt(0.0,PYB_OUTER_CIRCLE_START_COLOR); 39 | CircleGradient.setColorAt(1.0,PYB_OUTER_CIRCLE_END_COLOR); 40 | painter->setBrush(CircleGradient); 41 | 42 | painter->drawEllipse(CircleRect); 43 | 44 | painter->restore(); 45 | } 46 | 47 | void myAroundCircle::drawBMW(QPainter *painter) 48 | { 49 | // move to center 50 | 51 | // painter->translate(rect().center()); 52 | 53 | painter->save(); 54 | // setup transform 55 | QTransform t; 56 | t.translate(rect().center().x(),rect().center().y()); 57 | t.rotate(m_angle,Qt::ZAxis); 58 | painter->setTransform(t); 59 | 60 | qreal InnerRadius = m_outerRadius * PYB_RADIUS_FACTOR; 61 | QPointF tTopLeft( -InnerRadius,-InnerRadius); 62 | QPointF tBottomRight(InnerRadius,InnerRadius); 63 | QRectF tRect(tTopLeft,tBottomRight); 64 | 65 | qreal dAngle = 90 * 16; 66 | qreal StartAngle = 0; 67 | 68 | painter->setPen(Qt::NoPen); 69 | for(int AngleIndex = 0; AngleIndex < 4;AngleIndex++) 70 | { 71 | if(AngleIndex%2) 72 | { 73 | 74 | QRadialGradient PieGradient(tRect.center(),m_outerRadius,tRect.center()); 75 | PieGradient.setColorAt(0.0,PYB_BLUE_CIRCLE_START_COLOR); 76 | PieGradient.setColorAt(1.0,PYB_BLUE_CIRCLE_END_COLOR); 77 | painter->setBrush(PieGradient); 78 | 79 | }else{ 80 | 81 | QRadialGradient PieGradient(tRect.center(),m_outerRadius,tRect.center()); 82 | PieGradient.setColorAt(0.0,PYB_WHITE_CIRCLE_START_COLOR); 83 | PieGradient.setColorAt(1.0,PYB_WHITE_CIRCLE_END_COLOR); 84 | painter->setBrush(PieGradient); 85 | 86 | } 87 | painter->drawPie(tRect,StartAngle,dAngle); 88 | 89 | /// increment StartAngle 90 | StartAngle += dAngle; 91 | } 92 | 93 | painter->restore(); 94 | 95 | // 96 | } 97 | 98 | void myAroundCircle::UpdateAngle() 99 | { 100 | m_angle += 1; 101 | if(m_angle > 360) 102 | { 103 | m_angle = 0; 104 | } 105 | update(); 106 | } 107 | -------------------------------------------------------------------------------- /myaroundcircle.h: -------------------------------------------------------------------------------- 1 | #ifndef MYAROUNDCIRCLE_H 2 | #define MYAROUNDCIRCLE_H 3 | 4 | #define PYB_RADIUS_FACTOR 0.8 5 | 6 | #define PYB_OUTER_CIRCLE_START_COLOR QColor(65,65,65) 7 | #define PYB_OUTER_CIRCLE_END_COLOR QColor(89,89,89) 8 | 9 | #define PYB_BLUE_CIRCLE_START_COLOR QColor(0,133,203) 10 | #define PYB_BLUE_CIRCLE_END_COLOR QColor(0,118,177) 11 | 12 | #define PYB_WHITE_CIRCLE_START_COLOR QColor(255,255,255) 13 | #define PYB_WHITE_CIRCLE_END_COLOR QColor(233,233,233) 14 | 15 | #include 16 | #include 17 | 18 | class myAroundCircle : public QWidget 19 | { 20 | Q_OBJECT 21 | public: 22 | explicit myAroundCircle(QWidget *parent = 0); 23 | 24 | protected: 25 | void paintEvent(QPaintEvent *); 26 | 27 | QSize sizeHint() const 28 | { 29 | return QSize(300,300); 30 | } 31 | 32 | private: 33 | void drawUnderCircle(QPainter* painter); 34 | 35 | void drawBMW(QPainter* painter); 36 | 37 | 38 | private: 39 | QTimer* m_updateTimer; 40 | 41 | qreal m_angle; 42 | qreal m_outerRadius; 43 | 44 | private slots: 45 | void UpdateAngle(); 46 | 47 | }; 48 | 49 | #endif // MYAROUNDCIRCLE_H 50 | -------------------------------------------------------------------------------- /mybattery.cpp: -------------------------------------------------------------------------------- 1 | #include "mybattery.h" 2 | 3 | myBattery::myBattery(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | m_bReverse=false; 7 | m_value=100; 8 | updateTimer=new QTimer(this); 9 | updateTimer->setInterval(5); 10 | connect(updateTimer,SIGNAL(timeout()),this,SLOT(UpdateGraph())); 11 | } 12 | 13 | void myBattery::paintEvent(QPaintEvent *e) 14 | { 15 | QPainter painter; 16 | painter.begin(this); 17 | painter.setRenderHint(QPainter::Antialiasing); 18 | drawFrame(&painter); 19 | drawBattery(&painter); 20 | painter.end(); 21 | } 22 | void myBattery::drawFrame(QPainter *painter) 23 | { 24 | painter->save(); 25 | m_headWidth=width()/20; 26 | qreal batteryWidth=width()-m_headWidth; 27 | 28 | QPointF topLeftPot(0,0); 29 | QPointF bottomRightPot(batteryWidth,height()); 30 | QRectF batteryRect(topLeftPot,bottomRightPot); 31 | m_batteryRect=batteryRect; 32 | painter->setBrush(Qt::NoBrush); 33 | painter->setPen(QPen(Qt::black,2)); 34 | painter->drawRect(batteryRect); 35 | 36 | QPointF headRectTopLeft(batteryRect.topRight().x(),height()/4); 37 | QPointF headRectBottomRight(width(),height()-height()/4); 38 | QRectF headRect(headRectTopLeft,headRectBottomRight); 39 | QLinearGradient headRectGradient(headRect.topLeft(),headRect.bottomLeft()); 40 | headRectGradient.setColorAt(0.0,QColor(150,150,150)); 41 | headRectGradient.setColorAt(0.5,QColor(220,220,220)); 42 | headRectGradient.setColorAt(1.0,QColor(150,150,150)); 43 | 44 | painter->setBrush(headRectGradient); 45 | painter->drawRect(headRect); 46 | 47 | painter->restore(); 48 | } 49 | 50 | void myBattery::drawBattery(QPainter *painter) 51 | { 52 | painter->save(); 53 | 54 | QLinearGradient batteryGradient(QPointF(0,0),QPointF(0,height())); 55 | 56 | if(m_value<20) 57 | { 58 | batteryGradient.setColorAt(0.0,QColor(220,220,220)); 59 | batteryGradient.setColorAt(0.7,QColor(180,50,50)); 60 | batteryGradient.setColorAt(1.0,QColor(120,50,50)); 61 | 62 | } 63 | else 64 | { 65 | batteryGradient.setColorAt(0.0,QColor(220,220,220)); 66 | batteryGradient.setColorAt(0.7,QColor(0,180,0)); 67 | batteryGradient.setColorAt(1.0,QColor(0,120,0)); 68 | } 69 | painter->setPen(Qt::NoPen); 70 | painter->setBrush(batteryGradient); 71 | QPointF topLeftPot(m_batteryRect.topLeft().x()+2,m_batteryRect.topLeft().y()+2); 72 | 73 | qreal unit=m_batteryRect.width()/100; 74 | qreal wid=m_value*unit; 75 | QPointF bottomRightPot(wid,m_batteryRect.bottomRight().y()-2); 76 | painter->drawRect(QRectF(topLeftPot,bottomRightPot)); 77 | 78 | painter->restore(); 79 | 80 | } 81 | 82 | void myBattery::UpdateGraph() 83 | { 84 | if(m_bReverse) 85 | { 86 | m_value-=0.1; 87 | 88 | if(m_value<=m_currentValue) 89 | { 90 | updateTimer->stop(); 91 | } 92 | } 93 | else 94 | { 95 | m_value+=0.1; 96 | if(m_value>=m_currentValue) 97 | { 98 | updateTimer->stop(); 99 | } 100 | } 101 | emit valueChanged(m_value); 102 | 103 | update(); 104 | } 105 | 106 | void myBattery::setValue(qreal value) 107 | { 108 | if(value<0 || value>100) 109 | { 110 | qWarning() << "The value must be between 0 and 100 !"; 111 | return ; 112 | } 113 | if(value > m_value) 114 | { 115 | m_bReverse=false; 116 | } 117 | else if(valuestart(); 126 | m_currentValue=value; 127 | 128 | update(); 129 | } 130 | -------------------------------------------------------------------------------- /mybattery.h: -------------------------------------------------------------------------------- 1 | #ifndef MYBATTERY_H 2 | #define MYBATTERY_H 3 | 4 | #include 5 | #include 6 | 7 | class myBattery : public QWidget 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit myBattery(QWidget *parent = 0); 12 | 13 | protected: 14 | void paintEvent(QPaintEvent *); 15 | QSize sizeHint() const 16 | { 17 | return QSize(200,120); 18 | } 19 | 20 | 21 | private: 22 | void drawFrame(QPainter* painter); 23 | void drawBattery(QPainter* painter); 24 | 25 | private: 26 | QTimer* updateTimer; 27 | bool m_bReverse; 28 | qreal m_headWidth; 29 | qreal m_value; 30 | qreal m_currentValue; 31 | QRectF m_batteryRect; 32 | 33 | signals: 34 | void valueChanged(qreal value); 35 | 36 | private slots: 37 | void UpdateGraph(); 38 | 39 | public slots: 40 | void setValue(qreal value); 41 | 42 | }; 43 | 44 | #endif // MYBATTERY_H 45 | -------------------------------------------------------------------------------- /mybuttonboard.cpp: -------------------------------------------------------------------------------- 1 | #include "mybuttonboard.h" 2 | 3 | PYButtonBoardPrivate::PYButtonBoardPrivate(QWidget *parent) : 4 | QWidget(parent),m_MaxButtonLength(0),m_ButtonWidth(0) 5 | { 6 | setMouseTracking(true); 7 | } 8 | 9 | PYButtonBoardPrivate::~PYButtonBoardPrivate() 10 | { 11 | 12 | } 13 | 14 | void PYButtonBoardPrivate::paintEvent(QPaintEvent *) 15 | { 16 | QPainter painter(this); 17 | painter.setRenderHints(QPainter::Antialiasing|QPainter::HighQualityAntialiasing); 18 | drawBg(&painter); 19 | drawButtons(&painter); 20 | drawHoverAndSelectButton(&painter); 21 | } 22 | 23 | void PYButtonBoardPrivate::mouseMoveEvent(QMouseEvent *e) 24 | { 25 | int BtnCnt = m_ButtonList.count(); 26 | for(int BtnIndex = 0;BtnIndex < BtnCnt ; BtnIndex++) 27 | { 28 | if(m_RectTextPairVec.at(BtnIndex).first.contains(e->pos())) 29 | { 30 | m_HoverRect = m_RectTextPairVec.at(BtnIndex).first; 31 | m_strHoverText = m_RectTextPairVec.at(BtnIndex).second; 32 | update(); 33 | return ; 34 | } 35 | } 36 | } 37 | 38 | void PYButtonBoardPrivate::mousePressEvent(QMouseEvent *e) 39 | { 40 | int BtnCnt = m_ButtonList.count(); 41 | for(int BtnIndex = 0;BtnIndex < BtnCnt ; BtnIndex++) 42 | { 43 | if(m_RectTextPairVec.at(BtnIndex).first.contains(e->pos())) 44 | { 45 | m_SelectRect = m_RectTextPairVec.at(BtnIndex).first; 46 | m_strSelectText = m_RectTextPairVec.at(BtnIndex).second; 47 | update(); 48 | emit sig_ButtonClicked(m_strSelectText); 49 | return ; 50 | } 51 | } 52 | 53 | } 54 | void PYButtonBoardPrivate::leaveEvent(QEvent *) 55 | { 56 | m_HoverRect = QRectF(); 57 | m_strHoverText.clear(); 58 | update(); 59 | } 60 | 61 | void PYButtonBoardPrivate::drawBg(QPainter *painter) 62 | { 63 | painter->save(); 64 | painter->setPen(Qt::NoPen); 65 | QLinearGradient BgGradient(QPoint(0,0),QPoint(0,height())); 66 | BgGradient.setColorAt(0.0,PYBB_BG_START_COLOR); 67 | BgGradient.setColorAt(1.0,PYBB_BG_END_COLOR); 68 | painter->setBrush(BgGradient); 69 | painter->drawRect(rect()); 70 | painter->restore(); 71 | } 72 | 73 | void PYButtonBoardPrivate::drawButtons(QPainter *painter) 74 | { 75 | m_RectTextPairVec.clear(); 76 | 77 | painter->save(); 78 | 79 | int BtnCnt = m_ButtonList.count(); 80 | qreal ButtonWidth = m_MaxButtonLength + 2 * PYBB_EXTRA_SPACE; 81 | 82 | int hBoxCnt = (width() - PYBB_SPACE)/(ButtonWidth + PYBB_SPACE); 83 | 84 | qreal ExtraSpace = width() - hBoxCnt * ButtonWidth - (hBoxCnt + 1)*PYBB_SPACE; 85 | qreal SingleExtraSpace = (qreal)ExtraSpace/hBoxCnt; 86 | m_ButtonWidth = ButtonWidth + SingleExtraSpace; 87 | 88 | if(m_ButtonWidth < (m_MaxButtonLength + 2 * PYBB_EXTRA_SPACE)) 89 | { 90 | m_ButtonWidth = m_MaxButtonLength + 2 * PYBB_EXTRA_SPACE; 91 | } 92 | 93 | qreal initX = PYBB_SPACE; 94 | qreal initY = PYBB_SPACE; 95 | 96 | for(int FIndex = 1 ;FIndex <= BtnCnt ; FIndex++) 97 | { 98 | 99 | QPointF ButtonTopLeft(initX,initY); 100 | QPointF ButtonBottomRight(initX + m_ButtonWidth,initY + PYBB_BUTTON_HEIGHT); 101 | QRectF ButtonRect(ButtonTopLeft,ButtonBottomRight); 102 | 103 | m_RectTextPairVec.push_back(qMakePair(ButtonRect,m_ButtonList.at(FIndex-1))); 104 | 105 | 106 | painter->setPen(Qt::NoPen); 107 | QLinearGradient BoxGradient(ButtonRect.topLeft(),ButtonRect.bottomLeft()); 108 | BoxGradient.setColorAt(0.0,PYBB_BUTTON_START_COLOR); 109 | BoxGradient.setColorAt(1.0,PYBB_BUTTON_END_COLOR); 110 | painter->setBrush(BoxGradient); 111 | 112 | painter->drawRoundedRect(ButtonRect,PYBB_BUTTON_RECT_RADIUS,PYBB_BUTTON_RECT_RADIUS); 113 | 114 | 115 | // increment initX 116 | 117 | if(FIndex % hBoxCnt == 0 ) 118 | { 119 | // reset initX 120 | initX = PYBB_SPACE; 121 | 122 | // increment initY 123 | initY += PYBB_BUTTON_HEIGHT + PYBB_SPACE ; 124 | }else{ 125 | 126 | // increment initX 127 | initX += m_ButtonWidth + PYBB_SPACE; 128 | } 129 | 130 | painter->setPen(PYBB_NORMAL_TEXT_COLOR); 131 | painter->drawText(ButtonRect,tr("%1").arg(m_ButtonList.at(FIndex-1)),PYBB_BUTTON_TEXT_ALIGNMENT); 132 | 133 | } 134 | 135 | setMinimumHeight(initY + PYBB_SPACE + PYBB_BUTTON_HEIGHT ); 136 | painter->restore(); 137 | 138 | } 139 | 140 | void PYButtonBoardPrivate::drawHoverAndSelectButton(QPainter *painter) 141 | { 142 | 143 | // draw hover button and text 144 | if(!m_HoverRect.isEmpty()) 145 | { 146 | painter->save(); 147 | painter->setPen(Qt::NoPen); 148 | QLinearGradient HoverGradient(m_HoverRect.topLeft(),m_HoverRect.bottomLeft()); 149 | HoverGradient.setColorAt(0.0,PYBB_SELECT_BUTTON_START_COLOR); 150 | HoverGradient.setColorAt(1.0,PYBB_SELECT_BUTTON_END_COLOR); 151 | painter->setBrush(HoverGradient); 152 | painter->setOpacity(PYBB_BUTTON_HOVER_OPACITY); 153 | painter->drawRoundedRect(m_HoverRect,PYBB_BUTTON_RECT_RADIUS,PYBB_BUTTON_RECT_RADIUS); 154 | 155 | painter->setOpacity(1.0); 156 | painter->setPen(PYBB_HOVER_TEXT_COLOR); 157 | painter->drawText(m_HoverRect,tr("%1").arg(m_strHoverText),PYBB_BUTTON_TEXT_ALIGNMENT); 158 | painter->restore(); 159 | 160 | } 161 | 162 | // draw select button and text 163 | 164 | if(!m_strSelectText.isEmpty()) 165 | { 166 | int BtnCnt = m_RectTextPairVec.count(); 167 | for(int BtnIndex = 0;BtnIndex < BtnCnt ; BtnIndex++) 168 | { 169 | if(m_RectTextPairVec.at(BtnIndex).second == m_strSelectText) 170 | { 171 | m_SelectRect = m_RectTextPairVec.at(BtnIndex).first; 172 | break; 173 | } 174 | } 175 | // 176 | painter->save(); 177 | painter->setPen(Qt::NoPen); 178 | QFont BoldFont; 179 | BoldFont.setBold(true); 180 | painter->setFont(BoldFont); 181 | 182 | QLinearGradient SelectGradient(m_SelectRect.topLeft(),m_SelectRect.bottomLeft()); 183 | SelectGradient.setColorAt(0.0,PYBB_SELECT_BUTTON_START_COLOR); 184 | SelectGradient.setColorAt(1.0,PYBB_SELECT_BUTTON_END_COLOR); 185 | painter->setBrush(SelectGradient); 186 | painter->drawRoundedRect(m_SelectRect,PYBB_BUTTON_RECT_RADIUS,PYBB_BUTTON_RECT_RADIUS); 187 | 188 | painter->setPen(PYBB_SELECT_TEXT_COLOR); 189 | painter->drawText(m_SelectRect,tr("%1").arg(m_strSelectText),PYBB_BUTTON_TEXT_ALIGNMENT); 190 | painter->restore(); 191 | } 192 | } 193 | 194 | void PYButtonBoardPrivate::addButton(const QString &str) 195 | { 196 | qreal ButtonLength = fontMetrics().width(str); 197 | 198 | if(m_MaxButtonLength < ButtonLength) 199 | { 200 | m_MaxButtonLength = ButtonLength; 201 | } 202 | 203 | m_ButtonList.push_back(str); 204 | update(); 205 | } 206 | 207 | void PYButtonBoardPrivate::setButtons(const QStringList &strList) 208 | { 209 | m_ButtonList = strList; 210 | foreach(const QString& str,strList) 211 | { 212 | qreal ButtonLength = fontMetrics().width(str); 213 | if(m_MaxButtonLength < ButtonLength) 214 | { 215 | m_MaxButtonLength = ButtonLength; 216 | } 217 | } 218 | 219 | update(); 220 | } 221 | 222 | myButtonBoard::myButtonBoard(QWidget *parent):QWidget(parent) 223 | { 224 | createWidgets(); 225 | initSettings(); 226 | initMessages(); 227 | } 228 | 229 | void myButtonBoard::createWidgets() 230 | { 231 | d = new PYButtonBoardPrivate(this); 232 | dContainer = new QScrollArea(this); 233 | dContainer->setWidget(d); 234 | mainLayout = new QVBoxLayout(this); 235 | mainLayout->addWidget(dContainer); 236 | setLayout(mainLayout); 237 | 238 | } 239 | 240 | void myButtonBoard::initSettings() 241 | { 242 | dContainer->setWidgetResizable(true); 243 | dContainer->setContentsMargins(0,0,0,0); 244 | dContainer->setFrameShape(QFrame::NoFrame); 245 | 246 | } 247 | 248 | void myButtonBoard::initMessages() 249 | { 250 | connect(d,SIGNAL(sig_ButtonClicked(QString)),this,SIGNAL(sig_ButtonClicked(QString))); 251 | } 252 | 253 | void myButtonBoard::addButton(const QString &str) 254 | { 255 | d->addButton(str); 256 | } 257 | 258 | void myButtonBoard::setButtons(const QStringList &strList) 259 | { 260 | d->setButtons(strList); 261 | } 262 | -------------------------------------------------------------------------------- /mybuttonboard.h: -------------------------------------------------------------------------------- 1 | #ifndef MYBUTTONBOARD_H 2 | #define MYBUTTONBOARD_H 3 | 4 | #define DECLARE_PRIVATE(CLASS) class CLASS##Private; 5 | 6 | #ifdef PYBB_BUTTON_NORMAL_START_COLOR 7 | #undef PYBB_BUTTON_NORMAL_START_COLOR 8 | #endif 9 | #define PYBB_BUTTON_NORMAL_START_COLOR QColor(245,245,245) 10 | 11 | #ifdef PYBB_BUTTON_NORMAL_END_COLOR 12 | #undef PYBB_BUTTON_NORMAL_END_COLOR 13 | #endif 14 | #define PYBB_BUTTON_NORMAL_END_COLOR QColor(221,221,221) 15 | 16 | #ifdef PYBB_BUTTON_HIGHLIGHT_START_COLOR 17 | #undef PYBB_BUTTON_HIGHLIGHT_START_COLOR 18 | #endif 19 | #define PYBB_BUTTON_HIGHLIGHT_START_COLOR PYBB_BUTTON_HIGHLIGHT_END_COLOR.lighter() 20 | 21 | #ifdef PYBB_BUTTON_HIGHLIGHT_END_COLOR 22 | #undef PYBB_BUTTON_HIGHLIGHT_END_COLOR 23 | #endif 24 | #define PYBB_BUTTON_HIGHLIGHT_END_COLOR QColor(98,138,21) 25 | 26 | #ifdef PYBB_SPACE 27 | #undef PYBB_SPACE 28 | #endif 29 | #define PYBB_SPACE 5 30 | 31 | #ifdef PYBB_BUTTON_WIDTH 32 | #undef PYBB_BUTTON_WIDTH 33 | #endif 34 | #define PYBB_BUTTON_WIDTH 30 35 | 36 | #ifdef PYBB_BUTTON_START_COLOR 37 | #undef PYBB_BUTTON_START_COLOR 38 | #endif 39 | #define PYBB_BUTTON_START_COLOR PYBB_BUTTON_END_COLOR.lighter() 40 | 41 | #ifdef PYBB_BUTTON_END_COLOR 42 | #undef PYBB_BUTTON_END_COLOR 43 | #endif 44 | #define PYBB_BUTTON_END_COLOR QColor(221,221,221) 45 | 46 | #ifdef PYBB_BG_START_COLOR 47 | #undef PYBB_BG_START_COLOR 48 | #endif 49 | #define PYBB_BG_START_COLOR QColor(212,212,212) 50 | 51 | #ifdef PYBB_BG_END_COLOR 52 | #undef PYBB_BG_END_COLOR 53 | #endif 54 | #define PYBB_BG_END_COLOR QColor(198,198,198) 55 | 56 | /// BUTTON Macro 57 | 58 | #ifdef PYBB_BUTTON_RECT_RADIUS 59 | #undef PYBB_BUTTON_RECT_RADIUS 60 | #endif 61 | #define PYBB_BUTTON_RECT_RADIUS 5 62 | 63 | #ifdef PYBB_BUTTON_HOVER_OPACITY 64 | #undef PYBB_BUTTON_HOVER_OPACITY 65 | #endif 66 | #define PYBB_BUTTON_HOVER_OPACITY 0.6 67 | 68 | #ifdef PYBB_SELECT_BUTTON_START_COLOR 69 | #undef PYBB_SELECT_BUTTON_START_COLOR 70 | #endif 71 | #define PYBB_SELECT_BUTTON_START_COLOR PYBB_BUTTON_END_COLOR.lighter() 72 | 73 | #ifdef PYBB_SELECT_BUTTON_END_COLOR 74 | #undef PYBB_SELECT_BUTTON_END_COLOR 75 | #endif 76 | #define PYBB_SELECT_BUTTON_END_COLOR QColor(76,199,255) 77 | 78 | #ifdef PYBB_BUTTON_TEXT_ALIGNMENT 79 | #undef PYBB_BUTTON_TEXT_ALIGNMENT 80 | #endif 81 | #define PYBB_BUTTON_TEXT_ALIGNMENT Qt::AlignVCenter|Qt::AlignHCenter 82 | 83 | #ifdef PYBB_HOVER_TEXT_COLOR 84 | #undef PYBB_HOVER_TEXT_COLOR 85 | #endif 86 | #define PYBB_HOVER_TEXT_COLOR Qt::black 87 | 88 | #ifdef PYBB_SELECT_TEXT_COLOR 89 | #undef PYBB_SELECT_TEXT_COLOR 90 | #endif 91 | #define PYBB_SELECT_TEXT_COLOR Qt::white 92 | 93 | #ifdef PYBB_NORMAL_TEXT_COLOR 94 | #undef PYBB_NORMAL_TEXT_COLOR 95 | #endif 96 | #define PYBB_NORMAL_TEXT_COLOR Qt::black 97 | 98 | #ifdef PYBB_EXTRA_SPACE 99 | #undef PYBB_EXTRA_SPACE 100 | #endif 101 | #define PYBB_EXTRA_SPACE 7 102 | 103 | #ifdef PYBB_BUTTON_HEIGHT 104 | #undef PYBB_BUTTON_HEIGHT 105 | #endif 106 | #define PYBB_BUTTON_HEIGHT 25 107 | 108 | #include 109 | #include 110 | #include 111 | #include 112 | #include 113 | #include 114 | 115 | class PYButtonBoardPrivate : public QWidget 116 | { 117 | Q_OBJECT 118 | public: 119 | PYButtonBoardPrivate(QWidget* parent = 0); 120 | ~PYButtonBoardPrivate(); 121 | 122 | public: 123 | /// public interfaces 124 | void addButton(const QString& str); 125 | 126 | void setButtons(const QStringList& strList); 127 | 128 | 129 | protected: 130 | virtual void paintEvent(QPaintEvent *); 131 | 132 | QSize sizeHint() const 133 | { 134 | return QSize(500,200); 135 | } 136 | 137 | virtual void mouseMoveEvent(QMouseEvent *); 138 | 139 | virtual void mousePressEvent(QMouseEvent *); 140 | 141 | virtual void leaveEvent(QEvent *); 142 | private: 143 | /// painting functions 144 | void drawBg(QPainter* painter); 145 | 146 | void drawButtons(QPainter* painter); 147 | 148 | void drawHoverAndSelectButton(QPainter* painter); 149 | private: 150 | QStringList m_ButtonList; 151 | QVector > m_RectTextPairVec; 152 | 153 | qreal m_MaxButtonLength; 154 | qreal m_ButtonWidth; 155 | 156 | QRectF m_HoverRect; 157 | QRectF m_SelectRect; 158 | 159 | 160 | QString m_strHoverText; 161 | QString m_strSelectText; 162 | 163 | Q_SIGNALS: 164 | void sig_ButtonClicked(const QString& str); 165 | }; 166 | 167 | 168 | class myButtonBoard : public QWidget 169 | { 170 | Q_OBJECT 171 | public: 172 | explicit myButtonBoard(QWidget *parent = 0); 173 | 174 | signals: 175 | void sig_ButtonClicked(const QString& str); 176 | 177 | public: 178 | void addButton(const QString& str); 179 | void setButtons(const QStringList& strList); 180 | 181 | protected: 182 | QSize sizeHint() const 183 | { 184 | return QSize(500,200); 185 | } 186 | 187 | private: 188 | PYButtonBoardPrivate* d; 189 | QScrollArea* dContainer; 190 | QVBoxLayout* mainLayout; 191 | 192 | private: 193 | void createWidgets(); 194 | void initSettings(); 195 | void initMessages(); 196 | 197 | }; 198 | 199 | #endif // MYBUTTONBOARD_H 200 | -------------------------------------------------------------------------------- /mychart1.cpp: -------------------------------------------------------------------------------- 1 | #include "mychart1.h" 2 | 3 | myChart1::myChart1(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | this->initVariables(); 7 | } 8 | 9 | void myChart1::initVariables() 10 | { 11 | m_bUseAntialiasing=true; 12 | m_bShowHLine=true; 13 | m_bShowPoint=true; 14 | m_hLineClr=Qt::black; 15 | m_strTitle="Demo"; 16 | } 17 | 18 | void myChart1::paintEvent(QPaintEvent *e) 19 | { 20 | QPainter painter; 21 | painter.begin(this); 22 | 23 | if(m_bUseAntialiasing) 24 | { 25 | painter.setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing); 26 | } 27 | 28 | resetVariables(&painter); 29 | drawBackground(&painter); 30 | drawBox(&painter); 31 | drawText(&painter); 32 | drawGraph(&painter); 33 | drawTitle(&painter); 34 | } 35 | 36 | 37 | void myChart1::resetVariables(QPainter *painter) 38 | { 39 | QPointF topRectTopLeftPot(0,0); 40 | QPointF topRectBottomRightPot(width(),height()/3*2); 41 | m_topRect=QRectF(topRectTopLeftPot,topRectBottomRightPot); 42 | 43 | QPointF bottomRectTopLeftPot(0,height()/3*2); 44 | QPointF bottomRectBottomRightPot(width(),height()); 45 | m_bottomRect=QRectF(bottomRectTopLeftPot,bottomRectBottomRightPot); 46 | 47 | m_leftSpace=(qreal)width()/10; 48 | m_topSpace=(qreal)height()/10; 49 | } 50 | 51 | void myChart1::drawBackground(QPainter *painter) 52 | { 53 | painter->save(); 54 | painter->setPen(Qt::NoPen); 55 | QLinearGradient topGradient(m_topRect.topLeft(),m_topRect.bottomLeft()); 56 | topGradient.setColorAt(0.0,QColor(110,110,110)); 57 | topGradient.setColorAt(1.0,QColor(190,190,190)); 58 | painter->setBrush(topGradient); 59 | painter->drawRect(m_topRect); 60 | 61 | QLinearGradient bottomGradient(m_bottomRect.topLeft(),m_bottomRect.bottomLeft()); 62 | bottomGradient.setColorAt(0.0,QColor(190,190,190)); 63 | bottomGradient.setColorAt(1.0,QColor(130,130,130)); 64 | painter->setBrush(bottomGradient); 65 | painter->drawRect(m_bottomRect); 66 | painter->restore(); 67 | } 68 | 69 | void myChart1::drawBox(QPainter *painter) 70 | { 71 | painter->save(); 72 | QPointF topLeftPot(m_leftSpace,m_topSpace); 73 | QPointF bottomRightPot(width()-m_leftSpace,height()-m_topSpace); 74 | painter->setPen(Qt::black); 75 | painter->setBrush(Qt::NoBrush); 76 | 77 | m_boxRect=QRectF(topLeftPot,bottomRightPot); 78 | painter->drawRect(m_boxRect); 79 | 80 | if(m_bShowHLine) 81 | { 82 | painter->setPen(m_hLineClr); 83 | qreal increment=(qreal)m_boxRect.height()/10; 84 | qreal startY=m_boxRect.topLeft().y(); 85 | qreal boxWidth=m_boxRect.width(); 86 | for(int i=0;i<9;i++) 87 | { 88 | startY+=increment; 89 | QPointF leftPot(m_boxRect.topLeft().x(),startY); 90 | QPointF rightPot(m_boxRect.topRight().x(),startY); 91 | painter->setPen(Qt::DotLine); 92 | painter->drawLine(leftPot,rightPot); 93 | } 94 | } 95 | painter->restore(); 96 | } 97 | 98 | void myChart1::drawText(QPainter *painter) 99 | { 100 | painter->save(); 101 | 102 | int value=100; 103 | qreal increment=(qreal)m_boxRect.height()/10; 104 | qreal startY=m_boxRect.topLeft().y(); 105 | QString strValue; 106 | for(int i=0;i<=10;i++) 107 | { 108 | strValue=tr("%1%").arg(value); 109 | qreal textWidth=fontMetrics().width(strValue); 110 | qreal textHeight=fontMetrics().height(); 111 | 112 | QPointF textPot(m_boxRect.topLeft().x()-TEXT_BOX_SPACE-textWidth,startY+textHeight/2); 113 | painter->drawText(textPot,strValue); 114 | 115 | value-=10; 116 | startY+=increment; 117 | } 118 | 119 | painter->restore(); 120 | } 121 | 122 | void myChart1::drawGraph(QPainter *painter) 123 | { 124 | painter->save(); 125 | qreal startX=m_boxRect.topRight().x(); 126 | QVector potVec; 127 | painter->save(); 128 | painter->setPen(Qt::NoPen); 129 | potVec.push_back(QPointF(startX,m_boxRect.bottomRight().y())); 130 | for(int i=0;irestore(); 141 | 142 | QLinearGradient graphGradient(m_boxRect.topLeft(),m_boxRect.bottomLeft()); 143 | graphGradient.setColorAt(0.0,QColor(240,67,51)); 144 | graphGradient.setColorAt(0.4,QColor(208,94,49)); 145 | graphGradient.setColorAt(1.0,QColor(85,255,0)); 146 | painter->setBrush(graphGradient); 147 | painter->setPen(Qt::NoPen); 148 | painter->drawConvexPolygon(QPolygonF(potVec)); 149 | 150 | // draw points 151 | if(m_bShowPoint) 152 | { 153 | painter->setOpacity(0.5); 154 | for(int i=1;isetBrush(potGradient); 162 | painter->drawEllipse(dataPot,POINT_RADIUS,POINT_RADIUS); 163 | } 164 | } 165 | painter->restore(); 166 | } 167 | 168 | void myChart1::drawTitle(QPainter *painter) 169 | { 170 | painter->save(); 171 | qreal titleX=(qreal)width()/2; 172 | qreal titleY=m_topSpace/2; 173 | qreal textWidth=fontMetrics().width(m_strTitle); 174 | qreal textHeight=fontMetrics().height(); 175 | 176 | QFont titleFont; 177 | titleFont.setBold(true); 178 | titleFont.setPointSize(10); 179 | painter->setFont(titleFont); 180 | 181 | QPointF textPot(titleX-textWidth/2,titleY/2+textHeight/2); 182 | painter->drawText(textPot,m_strTitle); 183 | painter->restore(); 184 | } 185 | 186 | void myChart1::addData(qreal data) 187 | { 188 | m_dataVec.push_front(data); 189 | updateVector(); 190 | } 191 | 192 | void myChart1::updateVector() 193 | { 194 | int allowDataCount=m_boxRect.width()/X_INCREMENT; 195 | 196 | if(m_dataVec.count()>allowDataCount) 197 | { 198 | m_dataVec.remove(allowDataCount,1); 199 | } 200 | update(); 201 | } 202 | -------------------------------------------------------------------------------- /mychart1.h: -------------------------------------------------------------------------------- 1 | #ifndef MYCHART1_H 2 | #define MYCHART1_H 3 | 4 | #define TEXT_BOX_SPACE 5 5 | #define X_INCREMENT 5 6 | #define POINT_RADIUS 3 7 | 8 | #include 9 | #include 10 | 11 | class myChart1 : public QWidget 12 | { 13 | Q_OBJECT 14 | public: 15 | explicit myChart1(QWidget *parent = 0); 16 | 17 | public slots: 18 | void addData(qreal data); 19 | 20 | void setUseAntialiasing(bool use) 21 | { 22 | m_bUseAntialiasing=use; 23 | update(); 24 | } 25 | 26 | bool getUseAntialiasing() const 27 | { 28 | return m_bUseAntialiasing; 29 | } 30 | 31 | void setHorizontalLineColor(const QColor& clr) 32 | { 33 | m_hLineClr=clr; 34 | update(); 35 | } 36 | 37 | void setShowPoint(bool show) 38 | { 39 | m_bShowPoint=show; 40 | update(); 41 | } 42 | 43 | void setTitle(const QString& str) 44 | { 45 | m_strTitle=str; 46 | update(); 47 | } 48 | 49 | protected: 50 | void paintEvent(QPaintEvent *); 51 | 52 | void resizeEvent(QResizeEvent *) 53 | { 54 | updateVector(); 55 | } 56 | 57 | QSize sizeHint() const 58 | { 59 | return QSize(350,350); 60 | } 61 | 62 | QSize minimumSizeHint() const 63 | { 64 | return QSize(280,280); 65 | } 66 | 67 | private: 68 | void resetVariables(QPainter* painter); 69 | void drawBackground(QPainter* painter); 70 | void drawBox(QPainter* painter); 71 | void drawText(QPainter* painter); 72 | void drawGraph(QPainter* painter); 73 | void drawTitle(QPainter* painter); 74 | 75 | private: 76 | qreal m_leftSpace; 77 | qreal m_topSpace; 78 | QVector m_dataVec; 79 | QVector m_potVec; 80 | bool m_bUseAntialiasing; 81 | bool m_bShowHLine; 82 | bool m_bShowPoint; 83 | QRectF m_topRect; 84 | QRectF m_bottomRect; 85 | QRectF m_boxRect; 86 | QColor m_hLineClr; 87 | QString m_strTitle; 88 | 89 | void initVariables(); 90 | void updateVector(); 91 | 92 | }; 93 | 94 | #endif // MYCHART1_H 95 | -------------------------------------------------------------------------------- /mycoolbar.cpp: -------------------------------------------------------------------------------- 1 | #include "mycoolbar.h" 2 | 3 | myCoolBar::myCoolBar(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | this->initVariables(); 7 | } 8 | 9 | void myCoolBar::initVariables() 10 | { 11 | m_Min=0; 12 | m_Max=100; 13 | m_value=0; 14 | m_indicatorPos=0.0; 15 | m_pressed=false; 16 | m_valid=false; 17 | m_firstRun=true; 18 | 19 | updateTimer=new QTimer(this); 20 | updateTimer->setInterval(100); 21 | connect(updateTimer,SIGNAL(timeout()),this,SLOT(update())); 22 | updateTimer->start(); 23 | } 24 | 25 | void myCoolBar::setRange(int min, int max) 26 | { 27 | Q_ASSERT(max>=min); 28 | m_Min=min; 29 | m_Max=max; 30 | 31 | int length=m_Max-m_Min; 32 | m_increment=width()/length; 33 | update(); 34 | } 35 | 36 | void myCoolBar::setValue(int value) 37 | { 38 | m_value=value; 39 | update(); 40 | } 41 | 42 | void myCoolBar::paintEvent(QPaintEvent *e) 43 | { 44 | QPainter painter(this); 45 | painter.setRenderHint(QPainter::Antialiasing); 46 | drawBar(&painter); 47 | drawFilledRegion(&painter); 48 | drawIndicator(&painter); 49 | } 50 | 51 | void myCoolBar::drawBackground(QPainter *painter) 52 | { 53 | painter->save(); 54 | 55 | QLinearGradient lGradient(QPointF(0,0),QPointF(0,height())); 56 | lGradient.setColorAt(1.0,QColor(230,230,230)); 57 | lGradient.setColorAt(0.8,QColor(60,60,60)); 58 | lGradient.setColorAt(0.2,QColor(60,60,60)); 59 | lGradient.setColorAt(0.0,QColor(230,230,230)); 60 | 61 | painter->setBrush(lGradient); 62 | painter->drawRect(this->rect()); 63 | painter->restore(); 64 | } 65 | 66 | void myCoolBar::drawBar(QPainter *painter) 67 | { 68 | m_increment=width()/(m_Max-m_Min); 69 | painter->save(); 70 | qreal top=(qreal)(height()-BAR_HEIGHT)/2; 71 | qreal bottom=top+BAR_HEIGHT; 72 | QPointF topLeftPot(LEFT_WIDTH,top); 73 | QPointF bottomRightPot(width()-RIGHT_WIDTH,bottom); 74 | QRectF barRect(topLeftPot,bottomRightPot); 75 | m_barRect=barRect; 76 | 77 | QLinearGradient barGradient(barRect.topLeft(),barRect.bottomLeft()); 78 | barGradient.setColorAt(0.0,QColor(230,230,230)); 79 | barGradient.setColorAt(0.2,QColor(120,120,120)); 80 | barGradient.setColorAt(0.5,QColor(230,230,230)); 81 | barGradient.setColorAt(0.0,QColor(230,230,230)); 82 | painter->setBrush(barGradient); 83 | 84 | painter->drawRoundedRect(barRect,RECT_RADIUS,RECT_RADIUS); 85 | 86 | painter->restore(); 87 | } 88 | 89 | void myCoolBar::drawIndicator(QPainter *painter) 90 | { 91 | painter->save(); 92 | 93 | if(m_firstRun) 94 | { 95 | m_indicatorY=m_barRect.center().y(); 96 | m_indicatorX=m_barRect.topLeft().x()+m_indicatorPos+INDICATOR_RADIUS; 97 | QPointF indicatorCenter(m_indicatorX,m_indicatorY); 98 | m_lastPot=indicatorCenter; 99 | m_firstRun=false; 100 | } 101 | m_lastPot.setY(m_barRect.center().y()); 102 | 103 | QPointF indicatorTopLeftPot(m_lastPot.x()-INDICATOR_RADIUS,m_lastPot.y()-INDICATOR_RADIUS); 104 | QPointF indicatorBottomRightPot(m_lastPot.x()+INDICATOR_RADIUS,m_lastPot.y()+INDICATOR_RADIUS); 105 | 106 | QRectF indicatorRect(indicatorTopLeftPot,indicatorBottomRightPot); 107 | m_indicatorRect=indicatorRect; 108 | 109 | QLinearGradient indicatorGradient(indicatorRect.topLeft(),indicatorRect.bottomRight()); 110 | indicatorGradient.setColorAt(0.0,QColor(230,230,230)); 111 | indicatorGradient.setColorAt(0.4,QColor(120,120,120)); 112 | indicatorGradient.setColorAt(1.0,QColor(230,230,230)); 113 | painter->setBrush(indicatorGradient); 114 | painter->drawEllipse(indicatorRect.center(),INDICATOR_RADIUS,INDICATOR_RADIUS); 115 | 116 | painter->restore(); 117 | } 118 | 119 | void myCoolBar::drawFilledRegion(QPainter *painter) 120 | { 121 | if(m_lastPot.x()-m_barRect.x()<=INDICATOR_RADIUS) 122 | { 123 | return ; 124 | } 125 | 126 | painter->save(); 127 | QRectF regionRect(m_barRect.topLeft(),QPointF(m_indicatorRect.center().x(),m_indicatorRect.center().y()+m_barRect.height()/2)); 128 | QLinearGradient regionGradient(regionRect.topLeft(),regionRect.bottomLeft()); 129 | regionGradient.setColorAt(0.0,QColor(80,80,80)); 130 | regionGradient.setColorAt(0.2,QColor(120,120,120)); 131 | regionGradient.setColorAt(0.5,QColor(230,230,230)); 132 | regionGradient.setColorAt(0.8,QColor(120,120,120)); 133 | regionGradient.setColorAt(1.0,QColor(80,80,80)); 134 | painter->setPen(Qt::NoPen); 135 | painter->setBrush(regionGradient); 136 | painter->drawRoundedRect(regionRect,RECT_RADIUS,RECT_RADIUS); 137 | 138 | painter->restore(); 139 | } 140 | 141 | void myCoolBar::mousePressEvent(QMouseEvent *e) 142 | { 143 | if(e->button() & Qt::LeftButton) 144 | { 145 | if(m_indicatorRect.contains(e->pos())) 146 | { 147 | m_valid=true; 148 | } 149 | update(); 150 | } 151 | } 152 | 153 | void myCoolBar::mouseMoveEvent(QMouseEvent *e) 154 | { 155 | if(m_valid) 156 | { 157 | QPointF curPos=e->pos(); 158 | if(curPos.x()m_barRect.bottomRight().x()-INDICATOR_RADIUS/2) 159 | { 160 | return ; 161 | } 162 | 163 | if(curPos.x()>m_lastPot.x()) 164 | { 165 | m_lastPot=QPointF(e->pos().x(),m_barRect.center().y()); 166 | update(); 167 | } 168 | 169 | if(curPos.x()pos().x(),m_barRect.center().y()); 172 | update(); 173 | } 174 | } 175 | update(); 176 | 177 | } 178 | void myCoolBar::mouseReleaseEvent(QMouseEvent *e) 179 | { 180 | if(e->button() & Qt::LeftButton) 181 | { 182 | m_pressed=false; 183 | m_valid=false; 184 | update(); 185 | } 186 | } 187 | 188 | void myCoolBar::resizeEvent(QResizeEvent *) 189 | { 190 | update(); 191 | } 192 | 193 | -------------------------------------------------------------------------------- /mycoolbar.h: -------------------------------------------------------------------------------- 1 | #ifndef MYCOOLBAR_H 2 | #define MYCOOLBAR_H 3 | 4 | #include 5 | #include 6 | 7 | const int BAR_HEIGHT=25; 8 | const int INDICATOR_RADIUS=qRound(1.0*BAR_HEIGHT/2)+1; 9 | const int RECT_RADIUS =10; 10 | const int LEFT_WIDTH=20; 11 | const int RIGHT_WIDTH=LEFT_WIDTH; 12 | 13 | class myCoolBar : public QWidget 14 | { 15 | Q_OBJECT 16 | public: 17 | explicit myCoolBar(QWidget *parent = 0); 18 | void setRange(int min,int max); 19 | void setValue(int value); 20 | 21 | protected: 22 | void paintEvent(QPaintEvent *); 23 | QSize sizeHint() const 24 | { 25 | return QSize(300,30); 26 | } 27 | 28 | QSize minimumSizeHint() const 29 | { 30 | return QSize(200,30); 31 | } 32 | void mousePressEvent(QMouseEvent *); 33 | void mouseMoveEvent(QMouseEvent *); 34 | void mouseReleaseEvent(QMouseEvent *); 35 | void resizeEvent(QResizeEvent *); 36 | 37 | private: 38 | void drawBackground(QPainter* painter); 39 | void drawBar(QPainter* painter); 40 | void drawIndicator(QPainter* painter); 41 | void drawFilledRegion(QPainter* painter); 42 | 43 | 44 | private: 45 | qreal m_indicatorPos; 46 | int m_Max; 47 | int m_Min; 48 | int m_value; 49 | qreal m_increment; 50 | qreal m_indicatorY; 51 | qreal m_indicatorX; 52 | QRectF m_barRect; 53 | QRectF m_indicatorRect; 54 | QRectF m_leftRect; 55 | QPointF m_lastPot; 56 | bool m_pressed; 57 | bool m_valid; 58 | bool m_firstRun; 59 | QTimer* updateTimer; 60 | 61 | private: 62 | void initVariables(); 63 | 64 | signals: 65 | void valueChanged(int value); 66 | 67 | public slots: 68 | 69 | }; 70 | 71 | #endif // MYCOOLBAR_H 72 | -------------------------------------------------------------------------------- /myflashlist.cpp: -------------------------------------------------------------------------------- 1 | #include "myflashlist.h" 2 | 3 | myFlashList::myFlashList(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | initVars(); 7 | initWgts(); 8 | initStgs(); 9 | initConns(); 10 | } 11 | 12 | myFlashList::~myFlashList() 13 | { 14 | d->clear(); 15 | } 16 | 17 | void myFlashList::initVars() 18 | { 19 | } 20 | 21 | void myFlashList::initWgts() 22 | { 23 | mainLayout = new QHBoxLayout(this); 24 | scrollbar = new QScrollBar(this); 25 | d = new PYFlashListPrivate(this); 26 | 27 | mainLayout->addWidget(d); 28 | mainLayout->addWidget(scrollbar); 29 | setLayout(mainLayout); 30 | } 31 | 32 | void myFlashList::initStgs() 33 | { 34 | mainLayout->setContentsMargins(0,0,0,0); 35 | mainLayout->setSpacing(0); 36 | 37 | scrollbar->setRange(0,0); 38 | scrollbar->hide(); 39 | 40 | } 41 | 42 | void myFlashList::initConns() 43 | { 44 | connect(scrollbar,SIGNAL(valueChanged(int)),d,SLOT(setCurrentIndex(int))); 45 | connect(d,SIGNAL(sig_setMaximum(int)),this,SLOT(setMaximum(int))); 46 | connect(d,SIGNAL(sig_setCurrentIndex(int)),scrollbar,SLOT(setValue(int))); 47 | connect(d,SIGNAL(sig_itemClicked(QString)),this,SIGNAL(sig_itemClicked(QString))); 48 | } 49 | 50 | void myFlashList::calcGeo() 51 | { 52 | 53 | } 54 | 55 | 56 | void myFlashList::setMaximum(int max) 57 | { 58 | if(max == 0) 59 | { 60 | scrollbar->hide(); 61 | }else{ 62 | scrollbar->show(); 63 | } 64 | scrollbar->setRange(0,max); 65 | 66 | } 67 | 68 | 69 | void myFlashList::resizeEvent(QResizeEvent *) 70 | { 71 | 72 | } 73 | 74 | 75 | void myFlashList::addItem(const QString &str) 76 | { 77 | d->addItem(str); 78 | } 79 | 80 | void myFlashList::clear() 81 | { 82 | d->clear(); 83 | } 84 | 85 | void myFlashList::render() 86 | { 87 | d->render(); 88 | } 89 | 90 | 91 | PYFlashListPrivate::PYFlashListPrivate(QWidget* parent):QWidget(parent) 92 | { 93 | initVars(); 94 | initSettings(); 95 | } 96 | 97 | void PYFlashListPrivate::initVars() 98 | { 99 | m_currIndex = 0; 100 | m_VisibleItemCnt = 0; 101 | m_ItemCounter = 0; 102 | 103 | m_bAllJobsDone = false; 104 | 105 | m_RotateTimer = new QTimer(this); 106 | m_RotateTimer->setInterval(UPDATE_TIMER_INTERVAL); 107 | connect(m_RotateTimer,SIGNAL(timeout()),this,SLOT(DoRotation())); 108 | 109 | m_ItemCountTimer = new QTimer(this); 110 | m_ItemCountTimer->setInterval(ITEM_COUNTER_TIMER_INTERVAL); 111 | connect(m_ItemCountTimer,SIGNAL(timeout()),this,SLOT(UpdateItemCount())); 112 | } 113 | 114 | void PYFlashListPrivate::initSettings() 115 | { 116 | setMouseTracking(this); 117 | } 118 | 119 | void PYFlashListPrivate::calcGeo() 120 | { 121 | m_VisibleItemCnt = height()/ ITEM_HEIGHT + 1; 122 | int InvisibleItemCnt = m_IIVec.count() - m_VisibleItemCnt; 123 | 124 | if(InvisibleItemCnt >= 0) 125 | { 126 | emit sig_setMaximum(InvisibleItemCnt); 127 | } 128 | } 129 | 130 | void PYFlashListPrivate::makeupJobs() 131 | { 132 | int Guard = 0; 133 | for(int index = m_currIndex; index < m_IIVec.count() ;index++) 134 | { 135 | if(Guard++ > m_VisibleItemCnt) 136 | { 137 | break; 138 | } 139 | 140 | if(!m_IIVec.at(index).jobDone()) 141 | { 142 | if(!m_RotateTimer->isActive()) 143 | { 144 | m_RotateTimer->start(); 145 | m_ItemCountTimer->start(); 146 | break ; 147 | } 148 | } 149 | } 150 | 151 | // reset items which is before the current index 's angle 152 | for(int index = 0;index < m_currIndex ; index++) 153 | { 154 | m_IIVec[index].resetAngle(); 155 | } 156 | 157 | // reset items which is after the visible item's angle 158 | if((m_currIndex + m_VisibleItemCnt) < m_IIVec.count()) 159 | { 160 | for(int index = (m_currIndex + m_VisibleItemCnt);index < m_IIVec.count();index++) 161 | { 162 | m_IIVec[index].resetAngle(); 163 | } 164 | } 165 | 166 | } 167 | 168 | void PYFlashListPrivate::wheelUp() 169 | { 170 | if(--m_currIndex < 0) 171 | { 172 | m_currIndex = 0; 173 | } 174 | update(); 175 | emit sig_setCurrentIndex(m_currIndex); 176 | makeupJobs(); 177 | } 178 | 179 | void PYFlashListPrivate::wheelDown() 180 | { 181 | if(++m_currIndex + m_VisibleItemCnt > m_IIVec.count()) 182 | { 183 | m_currIndex = m_IIVec.count() - m_VisibleItemCnt; 184 | } 185 | update(); 186 | emit sig_setCurrentIndex(m_currIndex); 187 | makeupJobs(); 188 | } 189 | 190 | void PYFlashListPrivate::paintEvent(QPaintEvent *) 191 | { 192 | QPainter painter(this); 193 | painter.setRenderHints(QPainter::HighQualityAntialiasing|QPainter::Antialiasing); 194 | drawBg(&painter); 195 | drawItems(&painter); 196 | drawHoverRect(&painter); 197 | } 198 | 199 | void PYFlashListPrivate::mouseMoveEvent(QMouseEvent *e) 200 | { 201 | if(!m_bAllJobsDone) 202 | { 203 | return ; 204 | } 205 | 206 | int ItemCnt = e->pos().y()/ITEM_HEIGHT; 207 | if(m_currIndex + ItemCnt < m_IIVec.count()) 208 | { 209 | QPointF topLeft(0,ItemCnt*ITEM_HEIGHT); 210 | QPointF bottomRight(width(),(ItemCnt+1)*ITEM_HEIGHT); 211 | m_HoverRect = QRectF(topLeft,bottomRight); 212 | m_strHoverText = m_IIVec.at(m_currIndex+ItemCnt).getText(); 213 | update(); 214 | } 215 | } 216 | 217 | void PYFlashListPrivate::mousePressEvent(QMouseEvent *e) 218 | { 219 | if(!m_bAllJobsDone) 220 | { 221 | return ; 222 | } 223 | int ItemCnt = e->pos().y()/ITEM_HEIGHT; 224 | if(m_currIndex + ItemCnt < m_IIVec.count()) 225 | { 226 | m_selectedIndex = m_currIndex + ItemCnt; 227 | QString strText = m_IIVec.at(m_selectedIndex).getText(); 228 | emit sig_itemClicked(strText); 229 | } 230 | update(); 231 | } 232 | 233 | void PYFlashListPrivate::resizeEvent(QResizeEvent *) 234 | { 235 | 236 | calcGeo(); 237 | makeupJobs(); 238 | } 239 | 240 | void PYFlashListPrivate::leaveEvent(QEvent *) 241 | { 242 | m_HoverRect = QRectF(); 243 | update(); 244 | } 245 | void PYFlashListPrivate::showEvent(QShowEvent *) 246 | { 247 | calcGeo(); 248 | } 249 | 250 | void PYFlashListPrivate::wheelEvent(QWheelEvent *e) 251 | { 252 | if(e->delta() > 0) 253 | { 254 | wheelUp(); 255 | }else{ 256 | wheelDown(); 257 | } 258 | } 259 | 260 | void PYFlashListPrivate::drawBg(QPainter *painter) 261 | { 262 | painter->save(); 263 | painter->setPen(Qt::NoPen); 264 | QLinearGradient BgGradient(QPoint(0,0),QPoint(0,height())); 265 | BgGradient.setColorAt(0.0,BG_START_COLOR); 266 | BgGradient.setColorAt(1.0,BG_END_COLOR); 267 | painter->setBrush(BgGradient); 268 | painter->drawRect(rect()); 269 | painter->restore(); 270 | } 271 | 272 | void PYFlashListPrivate::drawItems(QPainter *painter) 273 | { 274 | if(m_IIVec.count() == 0) 275 | { 276 | return ; 277 | } 278 | 279 | painter->save(); 280 | 281 | int Guard = 0; 282 | qreal initY = 0; 283 | for(int index = m_currIndex;index < m_IIVec.count();index++) 284 | { 285 | if(Guard++ > m_VisibleItemCnt) 286 | { 287 | break ; 288 | } 289 | 290 | QPointF topLeft(0,initY); 291 | QPointF bottomRight(width(),initY + ITEM_HEIGHT); 292 | QRectF ItemRect(topLeft,bottomRight); 293 | 294 | painter->save(); 295 | QTransform t; 296 | t.translate(ItemRect.center().x(),ItemRect.center().y()); 297 | 298 | t.scale(m_IIVec.at(index).getZoomingFactor(),m_IIVec.at(index).getZoomingFactor()); 299 | painter->setTransform(t); 300 | 301 | QPointF TedTopLeft(-width()/2,-ITEM_HEIGHT/2); 302 | QPointF TedBottomRight(width()/2,ITEM_HEIGHT/2); 303 | QRectF TedRect(TedTopLeft,TedBottomRight); 304 | 305 | QLinearGradient ItemGradient(TedRect.topLeft(),TedRect.bottomLeft()); 306 | if(index%2) 307 | { 308 | ItemGradient.setColorAt(0.0,ITEM_START_COLOR); 309 | ItemGradient.setColorAt(1.0,ITEM_END_COLOR); 310 | }else{ 311 | ItemGradient.setColorAt(0.0,ITEM_START_COLOR_A); 312 | ItemGradient.setColorAt(1.0,ITEM_END_COLOR_A); 313 | } 314 | painter->setPen(Qt::NoPen); 315 | painter->setBrush(ItemGradient); 316 | painter->drawRect(TedRect); 317 | 318 | painter->setPen(NORMAL_TEXT_COLOR); 319 | painter->drawText(TedRect,m_IIVec.at(index).getText(),Qt::AlignVCenter|Qt::AlignHCenter); 320 | painter->restore(); 321 | /// increment initY 322 | initY += ITEM_HEIGHT; 323 | } 324 | painter->restore(); 325 | } 326 | 327 | void PYFlashListPrivate::drawHoverRect(QPainter *painter) 328 | { 329 | if(m_HoverRect.isEmpty() && !m_bAllJobsDone) 330 | { 331 | return ; 332 | } 333 | 334 | painter->save(); 335 | painter->setPen(Qt::NoPen); 336 | QLinearGradient HoverGradient(m_HoverRect.topLeft(),m_HoverRect.bottomLeft()); 337 | HoverGradient.setColorAt(0.0,HOVER_START_COLOR); 338 | HoverGradient.setColorAt(1.0,HOVER_END_COLOR); 339 | painter->setBrush(HoverGradient); 340 | painter->drawRoundedRect(m_HoverRect,RECT_RADIUS,RECT_RADIUS); 341 | 342 | QFont BoldFont; 343 | BoldFont.setBold(true); 344 | painter->setFont(BoldFont); 345 | 346 | painter->setPen(HOVER_TEXT_COLOR); 347 | painter->drawText(m_HoverRect,m_strHoverText,Qt::AlignVCenter|Qt::AlignHCenter); 348 | painter->restore(); 349 | } 350 | 351 | void PYFlashListPrivate::DoRotation() 352 | { 353 | int Guard = 0; 354 | bool bHasJobUndone = false; 355 | m_bAllJobsDone = false; 356 | m_HoverRect = QRectF(); 357 | 358 | for(int index = m_currIndex;index < m_IIVec.count()/* && Guard < m_ItemCounter*/ ;index++ ) 359 | { 360 | if(!m_IIVec.at(index).jobDone()) 361 | { 362 | bHasJobUndone = true; 363 | m_IIVec[index].updateZoomingFactor(); 364 | } 365 | 366 | if(Guard++ > m_VisibleItemCnt) 367 | { 368 | break; 369 | } 370 | update(); 371 | } 372 | 373 | if(!bHasJobUndone) 374 | { 375 | m_RotateTimer->stop(); 376 | m_bAllJobsDone = true; 377 | } 378 | 379 | } 380 | 381 | void PYFlashListPrivate::UpdateItemCount() 382 | { 383 | if(++m_ItemCounter > m_VisibleItemCnt) 384 | { 385 | if(m_ItemCountTimer->isActive()) 386 | { 387 | m_ItemCountTimer->stop(); 388 | m_ItemCounter = 0; 389 | } 390 | } 391 | } 392 | 393 | void PYFlashListPrivate::addItem(const QString &str) 394 | { 395 | ItemInfo ID(str); 396 | m_IIVec.push_back(ID); 397 | 398 | if(m_RotateTimer->isActive()) 399 | { 400 | m_RotateTimer->stop(); 401 | } 402 | m_RotateTimer->start(); 403 | 404 | } 405 | 406 | void PYFlashListPrivate::clear() 407 | { 408 | 409 | } 410 | 411 | void PYFlashListPrivate::render() 412 | { 413 | update(); 414 | } 415 | 416 | void PYFlashListPrivate::setCurrentIndex(int curIndex) 417 | { 418 | if(curIndex == m_currIndex) 419 | { 420 | return ; 421 | } 422 | 423 | m_currIndex = curIndex; 424 | 425 | m_VisibleItemCnt = height()/ITEM_HEIGHT + 1; 426 | int Guard = 0; 427 | for(int index = m_currIndex ;index < m_IIVec.count();index++) 428 | { 429 | m_IIVec[index].resetZoomingFactor(); 430 | if(Guard++ > m_VisibleItemCnt) 431 | { 432 | break ; 433 | } 434 | } 435 | m_RotateTimer->start(); 436 | m_ItemCountTimer->start(); 437 | } 438 | -------------------------------------------------------------------------------- /myflashlist.h: -------------------------------------------------------------------------------- 1 | #ifndef MYFLASHLIST_H 2 | #define MYFLASHLIST_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define ITEM_START_ANGLE 270 12 | #define ITEM_D_ANGLE 2 13 | #define ITEM_D_ZOOMING_FACTOR 0.05 14 | #define UPDATE_TIMER_INTERVAL 10 // ms 15 | #define ITEM_COUNTER_TIMER_INTERVAL 200 16 | #define SIZE_HINT QSize(300,300) 17 | 18 | #define ITEM_HEIGHT 70 19 | #define BG_START_COLOR QColor(89,89,89) 20 | #define BG_END_COLOR QColor(90,90,90) 21 | 22 | #define ITEM_START_COLOR ITEM_END_COLOR.lighter() 23 | #define ITEM_END_COLOR QColor(62,135,166) 24 | 25 | #define ITEM_START_COLOR_A ITEM_END_COLOR_A.lighter() 26 | #define ITEM_END_COLOR_A QColor(221,221,221) 27 | 28 | #define HOVER_OPACITY 0.2 29 | #define HOVER_START_COLOR HOVER_END_COLOR.lighter() 30 | #define HOVER_END_COLOR QColor(98,139,21) 31 | 32 | #define HOVER_TEXT_COLOR Qt::white 33 | #define SELECT_TEXT_COLOR Qt::white 34 | #define NORMAL_TEXT_COLOR Qt::black 35 | 36 | #define RECT_RADIUS 4 37 | 38 | class ItemInfo 39 | { 40 | public: 41 | ItemInfo(const QString& str):m_strData(str),m_Angle(ITEM_START_ANGLE),m_ZoomingFactor(0){} 42 | ItemInfo():m_Angle(ITEM_START_ANGLE),m_ZoomingFactor(0){} 43 | ~ItemInfo(){} 44 | 45 | public: 46 | /// public interfaces 47 | void setText(const QString& str) 48 | { 49 | m_strData = str; 50 | } 51 | 52 | QString getText() const 53 | { 54 | return m_strData; 55 | } 56 | 57 | void updateAngle() 58 | { 59 | m_Angle += ITEM_D_ANGLE; 60 | 61 | if(m_Angle > 360) 62 | { 63 | m_Angle = 0; 64 | } 65 | } 66 | 67 | void updateZoomingFactor() 68 | { 69 | m_ZoomingFactor += ITEM_D_ZOOMING_FACTOR; 70 | 71 | if(m_ZoomingFactor > 1.0) 72 | { 73 | m_ZoomingFactor = 1.0; 74 | } 75 | } 76 | 77 | bool jobDone() const 78 | { 79 | return m_Angle == 360 || m_ZoomingFactor == 1.0; 80 | } 81 | 82 | void resetAngle() 83 | { 84 | m_Angle = ITEM_START_ANGLE; 85 | } 86 | 87 | void resetZoomingFactor() 88 | { 89 | m_ZoomingFactor = 0.0; 90 | } 91 | 92 | qreal getAngle() const 93 | { 94 | return m_Angle; 95 | } 96 | 97 | qreal getZoomingFactor() const 98 | { 99 | return m_ZoomingFactor; 100 | } 101 | 102 | bool inWrongPosition() const 103 | { 104 | return m_Angle > ITEM_START_ANGLE && m_Angle < 360; 105 | } 106 | 107 | bool inWrongZoomingPosition() 108 | { 109 | return m_ZoomingFactor < 1.0; 110 | } 111 | 112 | private: 113 | QString m_strData; 114 | qreal m_Angle; 115 | qreal m_ZoomingFactor; 116 | }; 117 | class PYFlashListPrivate; 118 | 119 | class myFlashList : public QWidget 120 | { 121 | Q_OBJECT 122 | public: 123 | explicit myFlashList(QWidget *parent = 0); 124 | ~myFlashList(); 125 | 126 | public: 127 | /// proxy public interfaces for PYFlashListPrivate 128 | void addItem(const QString& str); 129 | 130 | void clear(); 131 | 132 | void render(); 133 | 134 | protected: 135 | QSize sizeHint() const 136 | { 137 | return QSize(SIZE_HINT); 138 | } 139 | 140 | void resizeEvent(QResizeEvent *); 141 | 142 | 143 | private: 144 | /// private utility functoins 145 | void initVars(); 146 | void initWgts(); 147 | void initStgs(); 148 | void initConns(); 149 | 150 | void calcGeo(); 151 | 152 | private Q_SLOTS: 153 | void setMaximum(int max); 154 | 155 | private: 156 | QHBoxLayout* mainLayout; 157 | QScrollBar* scrollbar; 158 | PYFlashListPrivate* d; 159 | 160 | Q_SIGNALS: 161 | void sig_setCurrentIndex(int currIndex); 162 | void sig_itemClicked(const QString& str); 163 | 164 | public slots: 165 | 166 | }; 167 | 168 | class PYFlashListPrivate : public QWidget 169 | { 170 | Q_OBJECT 171 | public: 172 | explicit PYFlashListPrivate(QWidget *parent = 0); 173 | 174 | public: 175 | /// public interfaces 176 | void addItem(const QString& str); 177 | 178 | void clear(); 179 | 180 | void render(); 181 | 182 | public Q_SLOTS: 183 | void setCurrentIndex(int curIndex); 184 | 185 | 186 | 187 | protected: 188 | void paintEvent(QPaintEvent *); 189 | 190 | void mouseMoveEvent(QMouseEvent *); 191 | 192 | void mousePressEvent(QMouseEvent *); 193 | 194 | void resizeEvent(QResizeEvent *); 195 | 196 | void leaveEvent(QEvent *); 197 | 198 | void showEvent(QShowEvent *); 199 | 200 | void wheelEvent(QWheelEvent *); 201 | 202 | private: 203 | /// painting functions 204 | void drawBg(QPainter* painter); 205 | 206 | void drawItems(QPainter* painter); 207 | 208 | void drawHoverRect(QPainter* painter); 209 | 210 | private: 211 | /// private utility functoins 212 | void initVars(); 213 | void initSettings(); 214 | void calcGeo(); 215 | void makeupJobs(); 216 | void wheelUp(); 217 | void wheelDown(); 218 | 219 | private: 220 | QVector m_IIVec; 221 | 222 | int m_currIndex; 223 | int m_selectedIndex; 224 | int m_VisibleItemCnt; 225 | int m_ItemCounter; 226 | 227 | bool m_bAllJobsDone; 228 | 229 | QRectF m_HoverRect; 230 | 231 | QString m_strHoverText; 232 | 233 | QTimer* m_RotateTimer; 234 | QTimer* m_ItemCountTimer; 235 | 236 | private Q_SLOTS: 237 | void DoRotation(); 238 | void UpdateItemCount(); 239 | 240 | signals: 241 | void sig_setMaximum(int max); 242 | void sig_setCurrentIndex(int currIndex); 243 | void sig_itemClicked(const QString& str); 244 | }; 245 | 246 | #endif // MYFLASHLIST_H 247 | -------------------------------------------------------------------------------- /mygauge1.cpp: -------------------------------------------------------------------------------- 1 | #include "mygauge1.h" 2 | 3 | myGauge1::myGauge1(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | m_value=0; 7 | m_currentValue=0; 8 | m_bReverse=false; 9 | updateTimer=new QTimer(this); 10 | updateTimer->setInterval(10); 11 | connect(updateTimer,SIGNAL(timeout()),this,SLOT(UpdateGraph())); 12 | 13 | m_outerRadius=width()>height() ? height()/2 : width()/2; 14 | m_innerRadius=0.8*m_outerRadius; 15 | m_colorPieRadius=m_outerRadius*0.7; 16 | m_coverCircleRadius=0.7*m_colorPieRadius; 17 | m_center=rect().center(); 18 | m_longHand=(qreal)m_outerRadius/20; 19 | m_okHand=0.7*m_longHand; 20 | m_shortHand=0.5*m_longHand; 21 | m_space=0.1*m_longHand; 22 | } 23 | 24 | void myGauge1::resetVariables(QPainter *painter) 25 | { 26 | m_outerRadius=width()>height() ? height()/2 : width()/2; 27 | m_innerRadius=0.8*m_outerRadius; 28 | m_center=rect().center(); 29 | m_colorPieRadius=m_outerRadius*0.7; 30 | m_coverCircleRadius=0.7*m_colorPieRadius; 31 | m_longHand=(qreal)m_outerRadius/20; 32 | m_okHand=0.7*m_longHand; 33 | m_shortHand=0.5*m_longHand; 34 | m_space=0.1*m_longHand; 35 | } 36 | 37 | void myGauge1::paintEvent(QPaintEvent *) 38 | { 39 | QPainter painter; 40 | painter.begin(this); 41 | painter.setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing); 42 | 43 | resetVariables(&painter); 44 | drawOuterCircle(&painter); 45 | drawInnerCircle(&painter); 46 | drawColorPies(&painter); 47 | drawGraph(&painter); 48 | drawCoverLines(&painter); 49 | drawCoverCircle(&painter); 50 | drawMarkAndText(&painter); 51 | 52 | drawTextRect(&painter); 53 | 54 | painter.end(); 55 | } 56 | 57 | void myGauge1::drawOuterCircle(QPainter* painter) 58 | { 59 | painter->save(); 60 | 61 | QRadialGradient outerGradient(m_center,m_outerRadius,m_center); 62 | outerGradient.setColorAt(0.0,QColor(130,130,130)); 63 | outerGradient.setColorAt(0.9,QColor(130,130,130)); 64 | outerGradient.setColorAt(0.95,QColor(200,200,200)); 65 | outerGradient.setColorAt(1.0,QColor(130,130,130)); 66 | 67 | painter->setPen(Qt::NoPen); 68 | painter->setBrush(outerGradient); 69 | painter->drawEllipse(m_center,m_outerRadius,m_outerRadius); 70 | 71 | painter->restore(); 72 | } 73 | 74 | void myGauge1::drawInnerCircle(QPainter* painter) 75 | { 76 | painter->save(); 77 | 78 | QRadialGradient innerGradient(m_center,m_innerRadius,m_center); 79 | innerGradient.setColorAt(0.0,QColor(180,180,180)); 80 | innerGradient.setColorAt(1.0,QColor(130,130,130)); 81 | 82 | painter->setPen(Qt::NoPen); 83 | painter->setBrush(innerGradient); 84 | painter->drawEllipse(m_center,m_innerRadius,m_innerRadius); 85 | 86 | painter->restore(); 87 | } 88 | 89 | void myGauge1::drawColorPies(QPainter* painter) 90 | { 91 | painter->save(); 92 | painter->setPen(Qt::NoPen); 93 | QPointF pieRectTopLeftPot(m_center.x()-m_colorPieRadius,m_center.y()-m_colorPieRadius); 94 | QPointF pieRectBottomRightPot(m_center.x()+m_colorPieRadius,m_center.y()+m_colorPieRadius); 95 | 96 | m_pieRect=QRectF(pieRectTopLeftPot,pieRectBottomRightPot); 97 | //draw green pie 98 | painter->setBrush(Qt::green); 99 | painter->drawPie(m_pieRect,72*16,108*16); 100 | // draw yellow pie 101 | painter->setBrush(Qt::yellow); 102 | painter->drawPie(m_pieRect,36*16,36*16); 103 | // draw blue pie 104 | painter->setBrush(Qt::red); 105 | painter->drawPie(m_pieRect,0*16,36*16); 106 | painter->restore(); 107 | } 108 | 109 | void myGauge1::drawCoverLines(QPainter* painter) 110 | { 111 | return ; 112 | painter->save(); 113 | 114 | qreal startAngle=90; 115 | qreal dAngle=(qreal)180/5; 116 | painter->translate(m_center); 117 | painter->setPen(QColor(255,0,127)); 118 | for(int i=0;i<=5;i++) 119 | { 120 | painter->save(); 121 | painter->rotate(startAngle); 122 | QPointF topPot(0,m_colorPieRadius); 123 | QPointF bottomPot(0,m_coverCircleRadius); 124 | painter->drawLine(topPot,bottomPot); 125 | painter->restore(); 126 | startAngle+=dAngle; 127 | } 128 | painter->restore(); 129 | } 130 | 131 | void myGauge1::drawCoverCircle(QPainter* painter) 132 | { 133 | painter->save(); 134 | painter->setPen(Qt::NoPen); 135 | 136 | QRadialGradient coverGradient(m_center,m_coverCircleRadius,m_center); 137 | coverGradient.setColorAt(0.0,QColor(180,180,180)); 138 | coverGradient.setColorAt(1.0,QColor(150,150,150)); 139 | 140 | painter->setBrush(coverGradient); 141 | painter->drawEllipse(m_center,m_coverCircleRadius,m_coverCircleRadius); 142 | 143 | painter->restore(); 144 | } 145 | 146 | void myGauge1::drawMarkAndText(QPainter* painter) 147 | { 148 | painter->save(); 149 | painter->translate(m_center); 150 | 151 | int value=0; 152 | QString strValue; 153 | qreal startAngle=90; 154 | qreal increment=(qreal)180/100; 155 | 156 | for(int i=0;i<=100;i++) 157 | { 158 | painter->save(); 159 | painter->rotate(startAngle); 160 | if(i%10==0) 161 | { 162 | painter->setPen(QColor(16,49,98)); 163 | QPointF bottomPot(0,m_colorPieRadius+m_space); 164 | QPointF topPot(0,m_colorPieRadius+m_space+m_longHand); 165 | painter->drawLine(bottomPot,topPot); 166 | 167 | strValue=tr("%1").arg(value); 168 | qreal textWidth=fontMetrics().width(strValue); 169 | qreal textHeight=fontMetrics().height(); 170 | QPointF textPot(-textWidth/2,m_colorPieRadius+2*m_space+m_longHand+textHeight); 171 | painter->save(); 172 | painter->translate(textPot); 173 | painter->rotate(180); 174 | painter->drawText(QPointF(-textWidth,textHeight/2),strValue); 175 | painter->restore(); 176 | value+=10; 177 | 178 | } 179 | else if(i%5==0) 180 | { 181 | 182 | painter->setPen(QColor(0,255,17)); 183 | QPointF bottomPot(0,m_colorPieRadius+m_space); 184 | QPointF topPot(0,m_colorPieRadius+m_space+m_okHand); 185 | painter->drawLine(bottomPot,topPot); 186 | } 187 | else 188 | { 189 | 190 | painter->setPen(QColor(255,170,0)); 191 | QPointF bottomPot(0,m_colorPieRadius+m_space); 192 | QPointF topPot(0,m_colorPieRadius+m_space+m_shortHand); 193 | painter->drawLine(bottomPot,topPot); 194 | } 195 | startAngle+=increment; 196 | painter->restore(); 197 | } 198 | painter->restore(); 199 | } 200 | 201 | void myGauge1::drawGraph(QPainter* painter) 202 | { 203 | qreal increment=(qreal)180/100; 204 | painter->save(); 205 | painter->setPen(Qt::NoPen); 206 | 207 | QRadialGradient graphGradient(m_center,m_colorPieRadius,m_center); 208 | graphGradient.setColorAt(0.0,QColor(180,180,180)); 209 | graphGradient.setColorAt(0.7,QColor(150,150,150)); 210 | graphGradient.setColorAt(0.85,QColor(180,180,180)); 211 | graphGradient.setColorAt(1.0,QColor(150,150,150)); 212 | painter->setBrush(graphGradient); 213 | painter->drawPie(m_pieRect,0,180*16-m_currentValue*increment*16); 214 | 215 | painter->restore(); 216 | } 217 | 218 | void myGauge1::drawTextRect(QPainter* painter) 219 | { 220 | painter->save(); 221 | qreal rectWidth=(m_colorPieRadius-m_coverCircleRadius)/2; 222 | 223 | painter->setOpacity(0.7); 224 | QPointF topLeftPot(m_center.x()-rectWidth,m_center.y()-rectWidth); 225 | QPointF bottomRightPot(m_center.x()+rectWidth,m_center.y()+rectWidth); 226 | QRectF textRect(topLeftPot,bottomRightPot); 227 | painter->setPen(Qt::NoPen); 228 | painter->setBrush(Qt::cyan); 229 | painter->drawRoundRect(textRect,rectWidth*2,rectWidth*2); 230 | 231 | painter->setOpacity(1.0); 232 | painter->setPen(Qt::black); 233 | qreal fontSize=rectWidth*0.75; 234 | QFont font; 235 | font.setPointSize(fontSize); 236 | painter->setFont(font); 237 | painter->drawText(textRect,Qt::AlignVCenter|Qt::AlignHCenter,tr("%1").arg(m_value)); 238 | painter->restore(); 239 | } 240 | 241 | void myGauge1::setValue(qreal value) 242 | { 243 | if(value>m_value) 244 | { 245 | m_bReverse=false; 246 | m_value=value; 247 | } 248 | else if(valuestart(); 258 | } 259 | 260 | void myGauge1::UpdateGraph() 261 | { 262 | if(m_bReverse) 263 | { 264 | m_currentValue-=0.5; 265 | 266 | if(m_currentValue<=m_value) 267 | { 268 | updateTimer->stop(); 269 | } 270 | } 271 | else 272 | { 273 | m_currentValue+=0.5; 274 | 275 | if(m_currentValue>=m_value) 276 | { 277 | updateTimer->stop(); 278 | } 279 | } 280 | update(); 281 | } 282 | -------------------------------------------------------------------------------- /mygauge1.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGAUGE1_H 2 | #define MYGAUGE1_H 3 | 4 | #include 5 | #include 6 | 7 | class myGauge1 : public QWidget 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit myGauge1(QWidget *parent = 0); 12 | 13 | protected: 14 | void paintEvent(QPaintEvent *); 15 | 16 | QSize sizeHint() const 17 | { 18 | return QSize(200,200); 19 | } 20 | 21 | QSize minimumSizeHint() const 22 | { 23 | return QSize(150,150); 24 | } 25 | 26 | private: 27 | void drawOuterCircle(QPainter* painter); 28 | void drawInnerCircle(QPainter* painter); 29 | void drawColorPies(QPainter* painter); 30 | void drawCoverLines(QPainter* painter); 31 | void drawCoverCircle(QPainter* painter); 32 | void drawMarkAndText(QPainter* painter); 33 | void drawGraph(QPainter* painter); 34 | void drawTextRect(QPainter* painter); 35 | void resetVariables(QPainter* painter); 36 | 37 | private: 38 | qreal m_value; 39 | qreal m_outerRadius; 40 | qreal m_innerRadius; 41 | qreal m_colorPieRadius; 42 | qreal m_coverCircleRadius; 43 | qreal m_currentValue; 44 | qreal m_longHand; 45 | qreal m_okHand; 46 | qreal m_shortHand; 47 | qreal m_space; 48 | bool m_bReverse; 49 | QTimer* updateTimer; 50 | QPointF m_center; 51 | QRectF m_pieRect; 52 | 53 | public slots: 54 | void setValue(qreal value); 55 | 56 | private slots: 57 | void UpdateGraph(); 58 | }; 59 | 60 | #endif // MYGAUGE1_H 61 | -------------------------------------------------------------------------------- /mygauge2.cpp: -------------------------------------------------------------------------------- 1 | #include "mygauge2.h" 2 | 3 | myGauge2::myGauge2(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | this->initVariables(); 7 | } 8 | 9 | void myGauge2::initVariables() 10 | { 11 | m_outerRadius=width()>height() ? height()/2: width()/2; 12 | m_innerRadius=m_outerRadius/8*7; 13 | m_coverCircleRadius=m_outerRadius/8*4+10; 14 | m_colorCircleRadius=m_outerRadius/8*5; 15 | m_center=rect().center(); 16 | m_value=0; 17 | m_currentValue=0; 18 | updateTimer=new QTimer(this); 19 | updateTimer->setInterval(10); 20 | connect(updateTimer,SIGNAL(timeout()),this,SLOT(UpdateGraph())); 21 | singleTimer=new QTimer(this); 22 | singleTimer->setInterval(100); 23 | connect(singleTimer,SIGNAL(timeout()),this,SLOT(update())); 24 | singleTimer->start(); 25 | } 26 | 27 | void myGauge2::paintEvent(QPaintEvent *) 28 | { 29 | QPainter painter(this); 30 | painter.setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing); 31 | resetVariables(&painter); 32 | drawOuterCircle(&painter); 33 | drawInnerCircle(&painter); 34 | drawColorPie(&painter); 35 | drawCoverCircle(&painter); 36 | drawMark(&painter); 37 | drawIndicator(&painter); 38 | drawCoverBall(&painter); 39 | drawTextRect(&painter); 40 | painter.end(); 41 | } 42 | 43 | void myGauge2::drawOuterCircle(QPainter *painter) 44 | { 45 | painter->save(); 46 | 47 | QRadialGradient outerGradient(m_center,m_outerRadius,m_center); 48 | outerGradient.setColorAt(0.0,QColor(200,200,200)); 49 | outerGradient.setColorAt(0.9,QColor(80,80,80)); 50 | outerGradient.setColorAt(0.95,QColor(180,180,180)); 51 | outerGradient.setColorAt(1.0,QColor(80,80,80)); 52 | 53 | painter->setPen(Qt::NoPen); 54 | painter->setBrush(outerGradient); 55 | painter->drawEllipse(m_center,m_outerRadius,m_outerRadius); 56 | painter->restore(); 57 | } 58 | 59 | void myGauge2::drawInnerCircle(QPainter *painter) 60 | { 61 | painter->save(); 62 | QRadialGradient innerGradient(m_center,m_innerRadius,m_center); 63 | innerGradient.setColorAt(0.0,QColor(130,130,130)); 64 | innerGradient.setColorAt(0.7,QColor(130,130,130)); 65 | innerGradient.setColorAt(1.0,QColor(80,80,80)); 66 | painter->setPen(Qt::NoPen); 67 | painter->setBrush(innerGradient); 68 | painter->drawEllipse(m_center,m_innerRadius,m_innerRadius); 69 | 70 | painter->restore(); 71 | } 72 | 73 | void myGauge2::drawMark(QPainter *painter) 74 | { 75 | painter->save(); 76 | painter->setPen(Qt::white); 77 | painter->translate(m_center); 78 | 79 | qreal dAngle=(qreal)270/100; 80 | qreal startAngle=45; 81 | int value=0; 82 | QString strValue; 83 | for(int i=0;i<=100;i++) 84 | { 85 | painter->save(); 86 | painter->rotate(startAngle); 87 | 88 | if(i%10==0) 89 | { 90 | strValue=tr("%1").arg(value); 91 | qreal textWidth=fontMetrics().width(strValue); 92 | qreal textHeight=fontMetrics().height(); 93 | QPointF bottomPot(0,m_colorCircleRadius+SPACE1); 94 | QPointF topPot(0,m_colorCircleRadius+SPACE1+LONG1); 95 | painter->drawLine(bottomPot,topPot); 96 | value+=10; 97 | 98 | painter->save(); 99 | QPointF textPot(0-textWidth/2,m_colorCircleRadius+SPACE1+LONG1+SPACE1+textHeight); 100 | painter->translate(textPot); 101 | painter->rotate(180); 102 | painter->drawText(QPointF(-textWidth,textHeight/2),strValue); 103 | painter->restore(); 104 | } 105 | else if(i%5==0) 106 | { 107 | QPointF bottomPot(0,m_colorCircleRadius+SPACE1); 108 | QPointF topPot(0,m_colorCircleRadius+SPACE1+OK1); 109 | painter->drawLine(bottomPot,topPot); 110 | 111 | } 112 | else 113 | { 114 | QPointF bottomPot(0,m_colorCircleRadius+SPACE1); 115 | QPointF topPot(0,m_colorCircleRadius+SPACE1+SHORT1); 116 | painter->drawLine(bottomPot,topPot); 117 | } 118 | painter->restore(); 119 | startAngle+=dAngle; 120 | } 121 | painter->restore(); 122 | } 123 | 124 | void myGauge2::drawCoverBall(QPainter *painter) 125 | { 126 | painter->save(); 127 | 128 | qreal ballRadius=m_outerRadius/7; 129 | m_coverBallRadius=ballRadius; 130 | QRadialGradient ballGradient(m_center,ballRadius,m_center); 131 | ballGradient.setColorAt(0.0,QColor(140,140,140)); 132 | ballGradient.setColorAt(0.7,QColor(140,140,140)); 133 | ballGradient.setColorAt(1.0,QColor(60,60,60)); 134 | painter->setBrush(ballGradient); 135 | painter->setPen(Qt::NoPen); 136 | painter->drawEllipse(m_center,ballRadius,ballRadius); 137 | 138 | painter->restore(); 139 | } 140 | 141 | void myGauge2::drawTextRect(QPainter *painter) 142 | { 143 | painter->save(); 144 | qreal rectWidth=m_coverCircleRadius/5; 145 | 146 | QPointF topLeftPot(m_center.x()-1.5*rectWidth,m_center.y()+rectWidth*2); 147 | QPointF bottomRightPot(topLeftPot.x()+3*rectWidth,topLeftPot.y()+rectWidth*2); 148 | QRectF textRect(topLeftPot,bottomRightPot); 149 | 150 | painter->setPen(Qt::NoPen); 151 | painter->setBrush(QColor(0,170,255)); 152 | painter->setOpacity(0.6); 153 | painter->drawRoundRect(textRect,ANGLE1,ANGLE1); 154 | 155 | qreal fontSize=textRect.height()/2; 156 | QFont font; 157 | font.setPointSize(fontSize); 158 | painter->setFont(font); 159 | 160 | painter->setOpacity(1.0); 161 | painter->setPen(Qt::black); 162 | QString strValue; 163 | strValue=tr("%1").arg(m_value); 164 | painter->drawText(textRect,Qt::AlignHCenter|Qt::AlignVCenter,strValue); 165 | painter->restore(); 166 | } 167 | 168 | void myGauge2::drawCoverCircle(QPainter *painter) 169 | { 170 | painter->save(); 171 | painter->setBrush(QColor(130,130,130)); 172 | painter->setPen(Qt::NoPen); 173 | painter->drawEllipse(m_center,m_coverCircleRadius,m_coverCircleRadius); 174 | painter->restore(); 175 | } 176 | 177 | void myGauge2::drawColorPie(QPainter *painter) 178 | { 179 | painter->save(); 180 | 181 | QPointF topLeftPot(m_center.x()-m_colorCircleRadius,m_center.y()-m_colorCircleRadius); 182 | QPointF bottomRightPot(m_center.x()+m_colorCircleRadius,m_center.y()+m_colorCircleRadius); 183 | m_colorCircleRect=QRectF(topLeftPot,bottomRightPot); 184 | painter->setPen(Qt::NoPen); 185 | 186 | QConicalGradient greenGradient(m_center,m_innerRadius); 187 | greenGradient.setColorAt(0.0,QColor(0,30,0)); 188 | greenGradient.setColorAt(0.25,QColor(0,230,0)); 189 | greenGradient.setColorAt(1.0,QColor(0,230,0)); 190 | painter->setBrush(Qt::green); 191 | painter->drawPie(m_colorCircleRect,45*16,180*16); 192 | 193 | painter->setBrush(QColor(218,218,0)); 194 | painter->drawPie(m_colorCircleRect,0*16,45*16); 195 | 196 | painter->setBrush(QColor(240,50,50)); 197 | painter->drawPie(m_colorCircleRect,0,-45*16); 198 | 199 | painter->restore(); 200 | } 201 | 202 | void myGauge2::drawIndicator(QPainter *painter) 203 | { 204 | painter->save(); 205 | 206 | painter->translate(m_center); 207 | qreal increment=(qreal)270/100; 208 | qreal changedAngle=45+increment*m_currentValue; 209 | painter->rotate(changedAngle); 210 | 211 | QPointF topPot(0,m_colorCircleRadius+LONG1); 212 | QPointF bottomLeftPot(-m_coverBallRadius/3,0); 213 | QPointF bottomRightPot(m_coverBallRadius/3,0); 214 | painter->setPen(Qt::NoPen); 215 | 216 | QLinearGradient indicatorGradient(topPot,bottomLeftPot); 217 | indicatorGradient.setColorAt(0.0,QColor(236,187,62)); 218 | indicatorGradient.setColorAt(0.5,QColor(220,147,0)); 219 | indicatorGradient.setColorAt(1.0,QColor(236,187,62)); 220 | 221 | painter->setBrush(indicatorGradient); 222 | QVector potVec; 223 | potVec.push_back(topPot); 224 | potVec.push_back(bottomLeftPot); 225 | potVec.push_back(bottomRightPot); 226 | 227 | painter->drawPolygon(potVec); 228 | painter->restore(); 229 | } 230 | 231 | void myGauge2::resetVariables(QPainter *painter) 232 | { 233 | m_outerRadius=width()>height() ? height()/2: width()/2; 234 | m_innerRadius=m_outerRadius/8*7; 235 | m_coverCircleRadius=m_outerRadius/8*4+10; 236 | m_colorCircleRadius=m_outerRadius/8*5; 237 | 238 | m_center=rect().center(); 239 | } 240 | 241 | void myGauge2::setValue(qreal value) 242 | { 243 | if(value>m_value) 244 | { 245 | m_bReverse=false; 246 | m_value=value; 247 | 248 | if(!m_bAnimating) 249 | { 250 | m_currentValue=m_value; 251 | } 252 | } 253 | else if(valuestart(); 267 | } 268 | 269 | void myGauge2::UpdateGraph() 270 | { 271 | if(!m_bReverse) 272 | { 273 | m_currentValue+=0.5; 274 | 275 | if(m_currentValue>=m_value) 276 | { 277 | updateTimer->stop(); 278 | } 279 | } 280 | else 281 | { 282 | m_currentValue-=0.5; 283 | if(m_currentValue<=m_value) 284 | { 285 | updateTimer->stop(); 286 | } 287 | } 288 | update(); 289 | } 290 | -------------------------------------------------------------------------------- /mygauge2.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGAUGE2_H 2 | #define MYGAUGE2_H 3 | 4 | #define LONG1 10 5 | #define OK1 7 6 | #define SHORT1 5 7 | #define SPACE1 3 8 | #define ANGLE1 10 9 | 10 | #include 11 | #include 12 | 13 | class myGauge2 : public QWidget 14 | { 15 | Q_OBJECT 16 | public: 17 | explicit myGauge2(QWidget *parent = 0); 18 | 19 | public slots: 20 | void setValue(qreal value); 21 | void setAnimating(bool enable) 22 | { 23 | m_bAnimating=enable; 24 | update(); 25 | } 26 | 27 | public: 28 | bool isAnimating() const 29 | { 30 | return m_bAnimating; 31 | } 32 | 33 | protected: 34 | void paintEvent(QPaintEvent *); 35 | 36 | QSize sizeHint() const 37 | { 38 | return QSize(300,300); 39 | } 40 | 41 | QSize minimumSizeHint() const 42 | { 43 | return QSize(200,200); 44 | } 45 | 46 | private: 47 | void drawOuterCircle(QPainter* painter); 48 | void drawInnerCircle(QPainter* painter); 49 | void drawMark(QPainter* painter); 50 | void drawCoverCircle(QPainter* painter); 51 | void drawColorPie(QPainter* painter); 52 | void drawIndicator(QPainter* painter); 53 | void drawCoverBall(QPainter* painter); 54 | void resetVariables(QPainter* painter); 55 | void drawTextRect(QPainter* painter); 56 | 57 | private: 58 | qreal m_outerRadius; 59 | qreal m_innerRadius; 60 | qreal m_coverCircleRadius; 61 | qreal m_colorCircleRadius; 62 | qreal m_coverBallRadius; 63 | QPointF m_center; 64 | QRectF m_colorCircleRect; 65 | qreal m_value; 66 | QTimer* updateTimer; 67 | bool m_bReverse; 68 | qreal m_currentValue; 69 | QTimer* singleTimer; 70 | bool m_bAnimating; 71 | 72 | private: 73 | void initVariables(); 74 | 75 | private slots: 76 | void UpdateGraph(); 77 | }; 78 | 79 | #endif // MYGAUGE2_H 80 | -------------------------------------------------------------------------------- /mygauge3.cpp: -------------------------------------------------------------------------------- 1 | #include "mygauge3.h" 2 | 3 | myGauge3::myGauge3(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | this->initVariables(); 7 | this->initSettings(); 8 | } 9 | 10 | void myGauge3::initVariables() 11 | { 12 | this->myTopColor=Qt::red; 13 | m_nMin=0; 14 | m_nMax=100; 15 | m_length=m_nMax-m_nMin; 16 | m_value=0; 17 | m_currentValue=0; 18 | tipTimer=new QTimer(this); 19 | tipTimer->setInterval(10); 20 | tipTimer->setSingleShot(true); 21 | 22 | eraseTimer=new QTimer(this); 23 | eraseTimer->setInterval(1500); 24 | eraseTimer->setSingleShot(true); 25 | 26 | connect(tipTimer,SIGNAL(timeout()),this,SLOT(ShowTip())); 27 | connect(eraseTimer,SIGNAL(timeout()),this,SLOT(HideTip())); 28 | 29 | m_longHand=height()/3; 30 | m_okHand=height()/4; 31 | m_shortHand=height()/5; 32 | m_handleWidth=width()/15; 33 | m_handleHeight=height()/5; 34 | m_indicatorLength=height()/5; 35 | 36 | m_bDrag=false; 37 | } 38 | 39 | void myGauge3::initSettings() 40 | { 41 | QPointF startPot(LEFT_SPACE1,m_longHand/2); 42 | m_indicatorLastPot=startPot; 43 | } 44 | 45 | void myGauge3::mousePressEvent(QMouseEvent *e) 46 | { 47 | if(e->button() & Qt::LeftButton) 48 | { 49 | if(m_handleRect.contains(e->pos())) 50 | { 51 | m_bDrag=true; 52 | update(); 53 | } 54 | } 55 | } 56 | 57 | void myGauge3::mouseReleaseEvent(QMouseEvent *e) 58 | { 59 | m_bDrag=false; 60 | update(); 61 | } 62 | 63 | void myGauge3::mouseMoveEvent(QMouseEvent *e) 64 | { 65 | if(m_bDrag) 66 | { 67 | m_lastPos=e->pos(); 68 | 69 | if(m_lastPos.x()>=m_lineLeftPot.x()&&m_lastPos.x()<=m_lineRightPot.x()) 70 | { 71 | QPointF indicatorTopPot(m_lastPos.x(),m_indicatorTopPot.y()); 72 | m_indicatorLastPot=indicatorTopPot; 73 | 74 | qreal totalLineWidth=m_lineRightPot.x()-m_lineLeftPot.x(); 75 | qreal dx=e->pos().x()-m_lineLeftPot.x(); 76 | qreal ratio=(qreal) dx/totalLineWidth; 77 | m_currentValue=m_length*ratio+m_nMin; 78 | emit valueChanged(m_currentValue); 79 | tipTimer->start(); 80 | update(); 81 | } 82 | } 83 | } 84 | 85 | void myGauge3::resizeEvent(QResizeEvent *e) 86 | { 87 | setValue(m_currentValue); 88 | e->accept(); 89 | } 90 | 91 | void myGauge3::paintEvent(QPaintEvent *) 92 | { 93 | QPainter painter; 94 | painter.begin(this); 95 | painter.setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing); 96 | 97 | resetVariables(&painter); 98 | drawBackground(&painter); 99 | drawRule(&painter); 100 | drawHandle(&painter); 101 | drawTip(&painter); 102 | painter.end(); 103 | } 104 | 105 | void myGauge3::drawBackground(QPainter *painter) 106 | { 107 | painter->save(); 108 | 109 | QLinearGradient bgGradient(QPointF(0,0),QPointF(0,height())); 110 | bgGradient.setColorAt(0.0,QColor(210,210,210)); 111 | bgGradient.setColorAt(0.1,QColor(50,50,50)); 112 | bgGradient.setColorAt(0.9,QColor(50,50,50)); 113 | bgGradient.setColorAt(1.0,QColor(210,210,210)); 114 | 115 | painter->setBrush(bgGradient); 116 | painter->setPen(Qt::NoPen); 117 | painter->drawRect(rect()); 118 | painter->restore(); 119 | } 120 | 121 | void myGauge3::drawRule(QPainter *painter) 122 | { 123 | painter->save(); 124 | drawHorizontalLine(painter); 125 | drawVerticalLines(painter); 126 | painter->restore(); 127 | } 128 | 129 | void myGauge3::drawHorizontalLine(QPainter *painter) 130 | { 131 | painter->save(); 132 | painter->setPen(Qt::white); 133 | 134 | QPointF leftPot(LEFT_SPACE1,TOP_SPACE1+m_longHand); 135 | m_lineLeftPot=leftPot; 136 | 137 | QPointF rightPot(width()-LEFT_SPACE1,TOP_SPACE1+m_longHand); 138 | m_lineRightPot=rightPot; 139 | painter->drawLine(leftPot,rightPot); 140 | painter->restore(); 141 | } 142 | 143 | void myGauge3::drawVerticalLines(QPainter *painter) 144 | { 145 | painter->save(); 146 | painter->setPen(Qt::white); 147 | 148 | int length=m_nMax-m_nMin; 149 | qreal startX=LEFT_SPACE1; 150 | qreal bottomY=m_lineLeftPot.y(); 151 | qreal lineLenght=m_lineRightPot.x()-m_lineLeftPot.x(); 152 | 153 | qreal increment=lineLenght/length; 154 | int startValue=m_nMin; 155 | QString strValue; 156 | for(int i=m_nMin;i<=m_nMax;i++) 157 | { 158 | if(i%10==0) 159 | { 160 | QPointF topPot(startX,bottomY-m_longHand); 161 | QPointF bottomPot(startX,bottomY); 162 | painter->drawLine(topPot,bottomPot); 163 | strValue=tr("%1").arg(startValue); 164 | qreal textWidth=fontMetrics().width(strValue); 165 | qreal textHeight=fontMetrics().height(); 166 | QPointF textPot(startX-textWidth/2,bottomY+textHeight); 167 | painter->drawText(textPot,strValue); 168 | 169 | startValue+=10; 170 | } 171 | else if(i%5==0) 172 | { 173 | QPointF topPot(startX,bottomY-m_okHand); 174 | QPointF bottomPot(startX,bottomY); 175 | painter->drawLine(topPot,bottomPot); 176 | } 177 | else 178 | { 179 | QPointF topPot(startX,bottomY-m_shortHand); 180 | QPointF bottomPot(startX,bottomY); 181 | painter->drawLine(topPot,bottomPot); 182 | } 183 | 184 | startX+=increment; 185 | } 186 | painter->restore(); 187 | } 188 | 189 | void myGauge3::drawTip(QPainter *painter) 190 | { 191 | if(!m_bShowTip) 192 | { 193 | return ; 194 | } 195 | painter->save(); 196 | QLinearGradient tipRectGradient(m_tipRect.topLeft(),m_tipRect.bottomLeft()); 197 | tipRectGradient.setColorAt(0.0,QColor(200,200,200)); 198 | tipRectGradient.setColorAt(1.0,QColor(170,170,170)); 199 | painter->setBrush(tipRectGradient); 200 | painter->drawRect(m_tipRect); 201 | painter->drawText(m_tipRect,Qt::AlignCenter,tr("%1").arg(m_currentValue)); 202 | 203 | painter->restore(); 204 | } 205 | 206 | void myGauge3::SetmyTopColor(QColor c) 207 | { 208 | this->myTopColor=c; 209 | } 210 | 211 | void myGauge3::drawHandle(QPainter *painter) 212 | { 213 | painter->save(); 214 | 215 | m_indicatorTopPot=QPointF(m_indicatorLastPot.x(),m_lineLeftPot.y()-m_longHand/4); 216 | QPointF indicatorLeftPot(m_indicatorTopPot.x()-width()/50,m_indicatorTopPot.y()+m_indicatorLength); 217 | m_indicatorBottomLeftPot=indicatorLeftPot; 218 | QPointF indicatorRightPot(m_indicatorTopPot.x()+width()/50,m_indicatorTopPot.y()+m_indicatorLength); 219 | m_indicatorBottomRightPot=indicatorRightPot; 220 | 221 | QLinearGradient indicatorGradient(indicatorLeftPot,indicatorRightPot); 222 | indicatorGradient.setColorAt(0.0,this->myTopColor); 223 | //indicatorGradient.setColorAt(0.0,QColor(180,50,50)); 224 | //indicatorGradient.setColorAt(0.5,QColor(230,30,30)); 225 | //indicatorGradient.setColorAt(1.0,QColor(180,50,50)); 226 | painter->setBrush(indicatorGradient); 227 | 228 | QVector potVec; 229 | potVec.push_back(m_indicatorTopPot); 230 | potVec.push_back(indicatorLeftPot); 231 | potVec.push_back(indicatorRightPot); 232 | 233 | painter->drawPolygon(potVec); 234 | 235 | qreal indicatorLength=indicatorRightPot.x()-indicatorLeftPot.x(); 236 | 237 | QPointF handleTopLeftPot(indicatorLeftPot.x()-(m_handleWidth/2-indicatorLength/2),indicatorLeftPot.y()); 238 | QPointF handleBottomRightPot(indicatorLeftPot.x()+indicatorLength,indicatorLeftPot.y()+m_handleHeight); 239 | QRectF handleRect(indicatorLeftPot,handleBottomRightPot); 240 | 241 | QPointF tipRectTopLeftPot(handleRect.topRight().x()+10,handleRect.topRight().y()); 242 | QString strValue; 243 | strValue=tr("%1").arg(m_currentValue); 244 | qreal textLength=fontMetrics().width(strValue); 245 | qreal textHeight=fontMetrics().height(); 246 | QPointF tipRectBottomRightPot(tipRectTopLeftPot.x()+textLength+5,tipRectTopLeftPot.y()+textHeight+5); 247 | QRectF tipRect(tipRectTopLeftPot,tipRectBottomRightPot); 248 | m_tipRect=tipRect; 249 | 250 | m_handleRect=handleRect; 251 | QLinearGradient handleGradient(handleRect.topLeft(),handleRect.topRight()); 252 | handleGradient.setColorAt(0.0,QColor(160,160,160)); 253 | handleGradient.setColorAt(0.2,QColor(200,200,200)); 254 | handleGradient.setColorAt(0.8,QColor(200,200,200)); 255 | handleGradient.setColorAt(1.0,QColor(160,160,160)); 256 | painter->setBrush(handleGradient); 257 | painter->drawRect(handleRect); 258 | painter->restore(); 259 | } 260 | 261 | void myGauge3::resetVariables(QPainter *painter) 262 | { 263 | m_longHand=height()/3; 264 | m_okHand=height()/4; 265 | m_shortHand=height()/5; 266 | m_handleWidth=width()/15; 267 | m_handleHeight=height()/5; 268 | m_indicatorLength=height()/5; 269 | } 270 | 271 | 272 | void myGauge3::setRange(int min, int max) 273 | { 274 | if(minstart(); 304 | update(); 305 | } 306 | 307 | void myGauge3::HideTip() 308 | { 309 | m_bShowTip=false; 310 | update(); 311 | } 312 | -------------------------------------------------------------------------------- /mygauge3.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGAUGE3_H 2 | #define MYGAUGE3_H 3 | 4 | #define LEFT_SPACE1 30 5 | #define TOP_SPACE1 40 6 | 7 | #include 8 | #include 9 | 10 | class myGauge3 : public QWidget 11 | { 12 | Q_OBJECT 13 | public: 14 | explicit myGauge3(QWidget *parent = 0); 15 | 16 | void SetmyTopColor(QColor c); 17 | 18 | protected: 19 | void paintEvent(QPaintEvent *); 20 | 21 | QSize sizeHint() const 22 | { 23 | return QSize(400,150); 24 | } 25 | 26 | QSize minimumSizeHint() const 27 | { 28 | return QSize(300,120); 29 | } 30 | 31 | void mousePressEvent(QMouseEvent *); 32 | void mouseReleaseEvent(QMouseEvent *); 33 | void mouseMoveEvent(QMouseEvent *); 34 | void resizeEvent(QResizeEvent *); 35 | 36 | private: 37 | void drawBackground(QPainter* painter); 38 | void drawRule(QPainter* painter); 39 | void drawHandle(QPainter* painter); 40 | void drawHorizontalLine(QPainter* painter); 41 | void drawVerticalLines(QPainter* painter); 42 | void resetVariables(QPainter* painter); 43 | void drawTip(QPainter* painter); 44 | 45 | private: 46 | int m_nMin; 47 | int m_nMax; 48 | int m_length; 49 | qreal m_value; 50 | qreal m_longHand; 51 | qreal m_okHand; 52 | qreal m_shortHand; 53 | qreal m_handleWidth; 54 | qreal m_handleHeight; 55 | qreal m_indicatorLength; 56 | QPointF m_indicatorTopPot; 57 | QPointF m_indicatorLastPot; 58 | QPointF m_indicatorBottomLeftPot; 59 | QPointF m_indicatorBottomRightPot; 60 | QPointF m_lineLeftPot; 61 | QPointF m_lineRightPot; 62 | QRectF m_handleRect; 63 | QPointF m_lastPos; 64 | QTimer* tipTimer; 65 | QTimer* eraseTimer; 66 | QRectF m_tipRect; 67 | qreal m_currentValue; 68 | 69 | bool m_bDrag; 70 | bool m_bShowTip; 71 | 72 | QColor myTopColor; 73 | 74 | private: 75 | void initVariables(); 76 | void initSettings(); 77 | 78 | private slots: 79 | void ShowTip(); 80 | void HideTip(); 81 | 82 | signals: 83 | void valueChanged(qreal value); 84 | 85 | public slots: 86 | void setRange(int min,int max); 87 | void setValue(qreal value); 88 | }; 89 | 90 | #endif // MYGAUGE3_H 91 | -------------------------------------------------------------------------------- /mygauge4.cpp: -------------------------------------------------------------------------------- 1 | #include "mygauge4.h" 2 | 3 | LinearGradient::LinearGradient(QObject *parent) : 4 | QObject(parent) 5 | { 6 | } 7 | 8 | QLinearGradient LinearGradient::GetGradient(const QPointF &startPos, const QPointF &endPos, const QColor &clr) 9 | { 10 | QLinearGradient gradient(startPos,endPos); 11 | int red=clr.red(); 12 | int green=clr.green(); 13 | int blue=clr.blue(); 14 | 15 | int startRed=red-INCREMENT2>=0 ? red-INCREMENT2 : 0; 16 | int startGreen=green-INCREMENT2>0 ? green-INCREMENT2 : 0; 17 | int startBlue=blue-INCREMENT2 >0? blue-INCREMENT2 : 0; 18 | 19 | QColor startClr(startRed,startGreen,startBlue); 20 | 21 | int endRed=red+INCREMENT2 < 255 ? red+INCREMENT2 : 255; 22 | int endGreen= green+INCREMENT2 < 255 ? green+INCREMENT2 : 255 ; 23 | int endBlue= blue+INCREMENT2 < 255 ? blue+INCREMENT2 : 255; 24 | 25 | QColor endClr(endRed,endGreen,endBlue); 26 | 27 | gradient.setColorAt(0.0,endClr); 28 | gradient.setColorAt(0.5,clr); 29 | gradient.setColorAt(1.0,endClr); 30 | 31 | return gradient; 32 | } 33 | 34 | myGauge4::myGauge4(QWidget *parent) : 35 | QWidget(parent) 36 | { 37 | this->mylinecolor=Qt::red; 38 | m_rectHeight=0; 39 | m_markLength=0; 40 | m_value=0; 41 | m_currentValue=0; 42 | m_bReverse=false; 43 | updateTimer=new QTimer(this); 44 | updateTimer->setInterval(10); 45 | connect(updateTimer,SIGNAL(timeout()),this,SLOT(UpdateGraph())); 46 | } 47 | 48 | void myGauge4::setValue(qreal value) 49 | { 50 | if(m_valuevalue) 55 | { 56 | m_bReverse=true; 57 | } 58 | else 59 | { 60 | return ; 61 | } 62 | m_value=value; 63 | updateTimer->start(); 64 | update(); 65 | } 66 | 67 | void myGauge4::paintEvent(QPaintEvent *) 68 | { 69 | QPainter painter; 70 | painter.begin(this); 71 | painter.setRenderHint(QPainter::Antialiasing); 72 | drawTopBottomRects(&painter); 73 | drawMark(&painter); 74 | drawIndicator(&painter); 75 | painter.end(); 76 | } 77 | 78 | void myGauge4::drawTopBottomRects(QPainter *painter) 79 | { 80 | painter->save(); 81 | m_rectHeight=height()/6; 82 | 83 | QPointF topLeftPot1(0,0); 84 | QPointF bottomRightPot1(width(),m_rectHeight); 85 | m_topRect=QRectF(topLeftPot1,bottomRightPot1); 86 | QLinearGradient topRectGradient(m_topRect.topLeft(),m_topRect.bottomLeft()); 87 | topRectGradient.setColorAt(0.0,QColor(180,180,180)); 88 | topRectGradient.setColorAt(1.0,QColor(80,80,80)); 89 | painter->setBrush(topRectGradient); 90 | painter->drawRect(m_topRect); 91 | 92 | QPointF topLeftPot2(0,height()-m_rectHeight); 93 | QPointF bottomRightPot2(width(),height()); 94 | m_bottomRect=QRectF(topLeftPot2,bottomRightPot2); 95 | QLinearGradient bottomRectGradient(m_bottomRect.topLeft(),m_bottomRect.bottomLeft()); 96 | bottomRectGradient.setColorAt(0.0,QColor(80,80,80)); 97 | bottomRectGradient.setColorAt(1.0,QColor(180,180,180)); 98 | painter->setBrush(bottomRectGradient); 99 | painter->drawRect(m_bottomRect); 100 | 101 | QPointF topLeftPot3(0,m_rectHeight); 102 | QPointF bottomRightPot3(width(),height()-m_rectHeight); 103 | m_middleRect=QRectF(topLeftPot3,bottomRightPot3); 104 | painter->setBrush(lg.GetGradient(m_middleRect.topLeft(),m_middleRect.bottomLeft(),QColor(90,90,90))); 105 | painter->drawRect(m_middleRect); 106 | 107 | painter->restore(); 108 | } 109 | 110 | void myGauge4::SetmyLineColor(QColor c) 111 | { 112 | this->mylinecolor=c; 113 | } 114 | 115 | void myGauge4::drawIndicator(QPainter *painter) 116 | { 117 | painter->save(); 118 | 119 | QPointF topPot(SPACE2+m_currentValue*m_increment,m_topY-TOP_SPACE2); 120 | QPointF bottomPot(SPACE2+m_currentValue*m_increment,m_bottomY+TOP_SPACE2); 121 | painter->setPen(this->mylinecolor); 122 | painter->drawLine(topPot,bottomPot); 123 | painter->restore(); 124 | } 125 | 126 | void myGauge4::drawMark(QPainter *painter) 127 | { 128 | painter->save(); 129 | m_markLength=m_middleRect.width()-2*SPACE2; 130 | m_length=m_markLength; 131 | qreal middleRectY1=m_middleRect.topLeft().y(); 132 | m_topY=middleRectY1+TOP_SPACE2; 133 | qreal middleRectY2=m_middleRect.bottomLeft().y(); 134 | m_bottomY=middleRectY2-TOP_SPACE2; 135 | 136 | qreal startX=m_middleRect.x()+SPACE2; 137 | m_startX=startX; 138 | qreal endX=m_middleRect.bottomRight().x()-SPACE2; 139 | qreal dx=m_markLength/100; 140 | m_increment=dx; 141 | painter->setPen(Qt::black); 142 | // draw two horizontal lines 143 | QPointF topLeftPot(startX,m_topY); 144 | QPointF topRightPot(endX,m_topY); 145 | painter->drawLine(topLeftPot,topRightPot); 146 | 147 | QPointF bottomLeftPot(startX,m_bottomY); 148 | QPointF bottomRightPot(endX,m_bottomY); 149 | painter->drawLine(bottomLeftPot,bottomRightPot); 150 | 151 | int value=0; 152 | int fontHeight=fontMetrics().height(); 153 | 154 | for(int i=0;i<=100;i++) 155 | { 156 | if(i%10==0) 157 | { 158 | QPointF topPot(startX,m_topY); 159 | QPointF bottomPot(startX,m_topY+LONG2); 160 | painter->drawLine(topPot,bottomPot); 161 | 162 | QPointF topPot1(startX,m_bottomY); 163 | QPointF bottomPot1(startX,m_bottomY-LONG2); 164 | painter->drawLine(topPot1,bottomPot1); 165 | 166 | QString strValue; 167 | strValue=tr("%1").arg(value); 168 | int fontWidth=fontMetrics().width(strValue)/2; 169 | painter->drawText(QPointF(startX-fontWidth,m_topY+fontHeight+LONG2),strValue); 170 | painter->drawText(QPointF(startX-fontWidth,m_bottomY-fontHeight),strValue); 171 | value+=10; 172 | 173 | } 174 | else if(i%5==0) 175 | { 176 | QPointF topPot(startX,m_topY); 177 | QPointF bottomPot(startX,m_topY+OK2); 178 | painter->drawLine(topPot,bottomPot); 179 | QPointF topPot1(startX,m_bottomY); 180 | QPointF bottomPot1(startX,m_bottomY-OK2); 181 | painter->drawLine(topPot1,bottomPot1); 182 | 183 | } 184 | else 185 | { 186 | QPointF topPot(startX,m_topY); 187 | QPointF bottomPot(startX,m_topY+SHORT2); 188 | painter->drawLine(topPot,bottomPot); 189 | QPointF topPot1(startX,m_bottomY); 190 | QPointF bottomPot1(startX,m_bottomY-SHORT2); 191 | painter->drawLine(topPot1,bottomPot1); 192 | } 193 | startX+=dx; 194 | } 195 | painter->restore(); 196 | } 197 | 198 | void myGauge4::UpdateGraph() 199 | { 200 | if(m_bReverse) 201 | { 202 | m_currentValue-=0.5; 203 | 204 | if(m_currentValue<=m_value) 205 | { 206 | updateTimer->stop(); 207 | } 208 | } 209 | else 210 | { 211 | m_currentValue+=0.5; 212 | 213 | if(m_currentValue>=m_value) 214 | { 215 | updateTimer->stop(); 216 | } 217 | } 218 | update(); 219 | } 220 | -------------------------------------------------------------------------------- /mygauge4.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGAUGE4_H 2 | #define MYGAUGE4_H 3 | 4 | #define INCREMENT2 66 5 | #define LONG2 8 6 | #define OK2 5 7 | #define SHORT2 3 8 | #define TOP_SPACE2 10 9 | #define SPACE2 20 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | class LinearGradient : public QObject 16 | { 17 | Q_OBJECT 18 | public: 19 | explicit LinearGradient(QObject *parent = 0); 20 | 21 | public: 22 | static QLinearGradient GetGradient(const QPointF& startPos,const QPointF& endPos,const QColor& clr); 23 | 24 | }; 25 | 26 | 27 | class myGauge4 : public QWidget 28 | { 29 | Q_OBJECT 30 | public: 31 | explicit myGauge4(QWidget *parent = 0); 32 | 33 | void SetmyLineColor(QColor c); 34 | 35 | protected: 36 | void paintEvent(QPaintEvent *); 37 | QSize sizeHint() const 38 | { 39 | return QSize(300,120); 40 | } 41 | 42 | 43 | private: 44 | void drawTopBottomRects(QPainter* painter); 45 | void drawMark(QPainter *painter); 46 | void drawIndicator(QPainter* painter); 47 | 48 | private: 49 | LinearGradient lg; 50 | qreal m_rectHeight; 51 | QRectF m_topRect; 52 | QRectF m_bottomRect; 53 | QRectF m_middleRect; 54 | qreal m_markLength; 55 | qreal m_topY; 56 | qreal m_startX; 57 | qreal m_length; 58 | qreal m_increment; 59 | qreal m_bottomY; 60 | bool m_bReverse; 61 | qreal m_value; 62 | qreal m_currentValue; 63 | QTimer* updateTimer; 64 | QColor mylinecolor; 65 | 66 | private slots: 67 | void UpdateGraph(); 68 | 69 | public slots: 70 | void setValue(qreal value); 71 | 72 | }; 73 | 74 | #endif // MYGAUGE4_H 75 | -------------------------------------------------------------------------------- /mygauge5.cpp: -------------------------------------------------------------------------------- 1 | #include "mygauge5.h" 2 | 3 | myGauge5::myGauge5(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | m_value=0; 7 | m_currentValue=0; 8 | m_bReverse=false; 9 | updateTimer=new QTimer(this); 10 | updateTimer->setInterval(10); 11 | connect(updateTimer,SIGNAL(timeout()),this,SLOT(UpdateGraph())); 12 | 13 | m_outerRadius=width()>height() ? height()/2 : width()/2; 14 | m_innerRadius=0.8*m_outerRadius; 15 | m_colorPieRadius=m_outerRadius*0.7; 16 | m_coverCircleRadius=0.7*m_colorPieRadius; 17 | m_center=rect().center(); 18 | m_longHand=(qreal)m_outerRadius/20; 19 | m_okHand=0.7*m_longHand; 20 | m_shortHand=0.5*m_longHand; 21 | m_space=0.1*m_longHand; 22 | } 23 | 24 | void myGauge5::resetVariables(QPainter *painter) 25 | { 26 | m_outerRadius=width()>height() ? height()/2 : width()/2; 27 | m_innerRadius=0.8*m_outerRadius; 28 | m_center=rect().center(); 29 | m_colorPieRadius=m_outerRadius*0.7; 30 | m_coverCircleRadius=0.7*m_colorPieRadius; 31 | m_longHand=(qreal)m_outerRadius/20; 32 | m_okHand=0.7*m_longHand; 33 | m_shortHand=0.5*m_longHand; 34 | m_space=0.1*m_longHand; 35 | } 36 | 37 | void myGauge5::paintEvent(QPaintEvent *) 38 | { 39 | QPainter painter; 40 | painter.begin(this); 41 | painter.setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing); 42 | resetVariables(&painter); 43 | drawOuterCircle(&painter); 44 | drawInnerCircle(&painter); 45 | drawColorPies(&painter); 46 | drawIndicator(&painter); 47 | drawCoverLines(&painter); 48 | drawCoverCircle(&painter); 49 | drawMarkAndText(&painter); 50 | drawTextRect(&painter); 51 | painter.end(); 52 | } 53 | 54 | void myGauge5::drawOuterCircle(QPainter* painter) 55 | { 56 | painter->save(); 57 | 58 | QRadialGradient outerGradient(m_center,m_outerRadius,m_center); 59 | outerGradient.setColorAt(0.0,QColor(130,130,130)); 60 | outerGradient.setColorAt(0.9,QColor(130,130,130)); 61 | outerGradient.setColorAt(0.95,QColor(200,200,200)); 62 | outerGradient.setColorAt(1.0,QColor(130,130,130)); 63 | 64 | painter->setPen(Qt::NoPen); 65 | painter->setBrush(outerGradient); 66 | painter->drawEllipse(m_center,m_outerRadius,m_outerRadius); 67 | 68 | painter->restore(); 69 | } 70 | 71 | void myGauge5::drawInnerCircle(QPainter* painter) 72 | { 73 | painter->save(); 74 | 75 | QRadialGradient innerGradient(m_center,m_innerRadius,m_center); 76 | innerGradient.setColorAt(0.0,QColor(180,180,180)); 77 | innerGradient.setColorAt(1.0,QColor(130,130,130)); 78 | 79 | painter->setPen(Qt::NoPen); 80 | painter->setBrush(innerGradient); 81 | painter->drawEllipse(m_center,m_innerRadius,m_innerRadius); 82 | 83 | painter->restore(); 84 | } 85 | 86 | void myGauge5::drawColorPies(QPainter* painter) 87 | { 88 | painter->save(); 89 | painter->setPen(Qt::NoPen); 90 | 91 | QPointF pieRectTopLeftPot(m_center.x()-m_colorPieRadius,m_center.y()-m_colorPieRadius); 92 | QPointF pieRectBottomRightPot(m_center.x()+m_colorPieRadius,m_center.y()+m_colorPieRadius); 93 | 94 | m_pieRect=QRectF(pieRectTopLeftPot,pieRectBottomRightPot); 95 | 96 | //draw green pie 97 | painter->setBrush(Qt::green); 98 | painter->drawPie(m_pieRect,72*16,108*16); 99 | 100 | // draw yellow pie 101 | painter->setBrush(Qt::yellow); 102 | painter->drawPie(m_pieRect,36*16,36*16); 103 | 104 | // draw blue pie 105 | painter->setBrush(Qt::red); 106 | painter->drawPie(m_pieRect,0*16,36*16); 107 | painter->restore(); 108 | 109 | } 110 | 111 | void myGauge5::drawCoverLines(QPainter* painter) 112 | { 113 | painter->save(); 114 | 115 | qreal startAngle=90; 116 | qreal dAngle=(qreal)180/5; 117 | painter->translate(m_center); 118 | painter->setPen(QColor(100,100,100)); 119 | for(int i=0;i<=5;i++) 120 | { 121 | 122 | painter->save(); 123 | painter->rotate(startAngle); 124 | QPointF topPot(0,m_colorPieRadius); 125 | QPointF bottomPot(0,m_coverCircleRadius); 126 | painter->drawLine(topPot,bottomPot); 127 | 128 | painter->restore(); 129 | startAngle+=dAngle; 130 | } 131 | painter->restore(); 132 | 133 | } 134 | 135 | void myGauge5::drawCoverCircle(QPainter* painter) 136 | { 137 | painter->save(); 138 | painter->setPen(Qt::NoPen); 139 | 140 | QRadialGradient coverGradient(m_center,m_coverCircleRadius,m_center); 141 | coverGradient.setColorAt(0.0,QColor(180,180,180)); 142 | coverGradient.setColorAt(1.0,QColor(150,150,150)); 143 | 144 | painter->setBrush(coverGradient); 145 | painter->drawEllipse(m_center,m_coverCircleRadius,m_coverCircleRadius); 146 | 147 | painter->restore(); 148 | } 149 | 150 | void myGauge5::drawMarkAndText(QPainter* painter) 151 | { 152 | painter->save(); 153 | painter->translate(m_center); 154 | 155 | int value=0; 156 | QString strValue; 157 | qreal startAngle=90; 158 | qreal increment=(qreal)180/100; 159 | for(int i=0;i<=100;i++) 160 | { 161 | painter->save(); 162 | painter->rotate(startAngle); 163 | if(i%10==0) 164 | { 165 | painter->setPen(QColor(16,49,98)); 166 | QPointF bottomPot(0,m_colorPieRadius+m_space); 167 | QPointF topPot(0,m_colorPieRadius+m_space+m_longHand); 168 | painter->drawLine(bottomPot,topPot); 169 | 170 | strValue=tr("%1").arg(value); 171 | qreal textWidth=fontMetrics().width(strValue); 172 | qreal textHeight=fontMetrics().height(); 173 | QPointF textPot(-textWidth/2,m_colorPieRadius+2*m_space+m_longHand+textHeight); 174 | painter->save(); 175 | painter->translate(textPot); 176 | painter->rotate(180); 177 | painter->drawText(QPointF(-textWidth,textHeight/2),strValue); 178 | painter->restore(); 179 | value+=10; 180 | } 181 | else if(i%5==0) 182 | { 183 | 184 | painter->setPen(QColor(0,255,17)); 185 | QPointF bottomPot(0,m_colorPieRadius+m_space); 186 | QPointF topPot(0,m_colorPieRadius+m_space+m_okHand); 187 | painter->drawLine(bottomPot,topPot); 188 | } 189 | else 190 | { 191 | 192 | painter->setPen(QColor(255,170,0)); 193 | QPointF bottomPot(0,m_colorPieRadius+m_space); 194 | QPointF topPot(0,m_colorPieRadius+m_space+m_shortHand); 195 | painter->drawLine(bottomPot,topPot); 196 | } 197 | 198 | startAngle+=increment; 199 | painter->restore(); 200 | } 201 | painter->restore(); 202 | } 203 | 204 | void myGauge5::drawIndicator(QPainter* painter) 205 | { 206 | painter->save(); 207 | painter->setPen(QPen(Qt::blue,2)); 208 | painter->translate(m_center); 209 | 210 | qreal increment=(qreal)180/100; 211 | qreal endAngle=90+m_currentValue*increment; 212 | painter->rotate(endAngle); 213 | painter->drawLine(QPointF(0,0),QPointF(0,m_colorPieRadius+m_longHand)); 214 | 215 | painter->restore(); 216 | } 217 | 218 | void myGauge5::drawTextRect(QPainter* painter) 219 | { 220 | painter->save(); 221 | qreal rectWidth=(m_colorPieRadius-m_coverCircleRadius)/2; 222 | 223 | painter->setOpacity(0.7); 224 | QPointF topLeftPot(m_center.x()-rectWidth,m_center.y()-rectWidth); 225 | QPointF bottomRightPot(m_center.x()+rectWidth,m_center.y()+rectWidth); 226 | QRectF textRect(topLeftPot,bottomRightPot); 227 | painter->setPen(Qt::NoPen); 228 | painter->setBrush(Qt::cyan); 229 | painter->drawRoundRect(textRect,rectWidth*2,rectWidth*2); 230 | 231 | painter->setOpacity(1.0); 232 | painter->setPen(Qt::black); 233 | qreal fontSize=rectWidth*0.75; 234 | QFont font; 235 | font.setPointSize(fontSize); 236 | painter->setFont(font); 237 | painter->drawText(textRect,Qt::AlignVCenter|Qt::AlignHCenter,tr("%1").arg(m_value)); 238 | painter->restore(); 239 | } 240 | 241 | void myGauge5::setValue(qreal value) 242 | { 243 | if(value>m_value) 244 | { 245 | m_bReverse=false; 246 | m_value=value; 247 | } 248 | else if(valuestart(); 258 | } 259 | 260 | void myGauge5::UpdateGraph() 261 | { 262 | if(m_bReverse) 263 | { 264 | m_currentValue-=0.5; 265 | 266 | if(m_currentValue<=m_value) 267 | { 268 | updateTimer->stop(); 269 | } 270 | } 271 | else 272 | { 273 | m_currentValue+=0.5; 274 | if(m_currentValue>=m_value) 275 | { 276 | updateTimer->stop(); 277 | } 278 | } 279 | update(); 280 | } 281 | -------------------------------------------------------------------------------- /mygauge5.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGAUGE5_H 2 | #define MYGAUGE5_H 3 | 4 | #include 5 | #include 6 | 7 | class myGauge5 : public QWidget 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit myGauge5(QWidget *parent = 0); 12 | 13 | protected: 14 | void paintEvent(QPaintEvent *); 15 | 16 | QSize sizeHint() const 17 | { 18 | return QSize(200,200); 19 | } 20 | 21 | QSize minimumSizeHint() const 22 | { 23 | return QSize(150,150); 24 | } 25 | 26 | private: 27 | void drawOuterCircle(QPainter* painter); 28 | void drawInnerCircle(QPainter* painter); 29 | void drawColorPies(QPainter* painter); 30 | void drawCoverLines(QPainter* painter); 31 | void drawCoverCircle(QPainter* painter); 32 | void drawMarkAndText(QPainter* painter); 33 | void drawIndicator(QPainter* painter); 34 | void drawTextRect(QPainter* painter); 35 | void resetVariables(QPainter* painter); 36 | 37 | 38 | private: 39 | qreal m_value; 40 | qreal m_outerRadius; 41 | qreal m_innerRadius; 42 | qreal m_colorPieRadius; 43 | qreal m_coverCircleRadius; 44 | qreal m_currentValue; 45 | qreal m_longHand; 46 | qreal m_okHand; 47 | qreal m_shortHand; 48 | qreal m_space; 49 | bool m_bReverse; 50 | QTimer* updateTimer; 51 | QPointF m_center; 52 | QRectF m_pieRect; 53 | 54 | private slots: 55 | void UpdateGraph(); 56 | 57 | public slots: 58 | void setValue(qreal value); 59 | 60 | }; 61 | 62 | #endif // MYGAUGE5_H 63 | -------------------------------------------------------------------------------- /mygauge6.cpp: -------------------------------------------------------------------------------- 1 | #include "mygauge6.h" 2 | 3 | myGauge6::myGauge6(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | graphcolor=Qt::darkGreen; 7 | m_currentValue=0; 8 | m_value=0; 9 | m_bReverse=false; 10 | m_bUseAntialiasing=false; 11 | m_bAnimate=true; 12 | m_nMin=0; 13 | m_nMax=100; 14 | 15 | updateTimer=new QTimer(this); 16 | updateTimer->setInterval(30); 17 | connect(updateTimer,SIGNAL(timeout()),this,SLOT(UpdateGraph())); 18 | 19 | } 20 | 21 | void myGauge6::paintEvent(QPaintEvent *) 22 | { 23 | QPainter painter; 24 | painter.begin(this); 25 | 26 | if(m_bUseAntialiasing) 27 | { 28 | painter.setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing); 29 | } 30 | 31 | drawVariables(&painter); 32 | drawBackground(&painter); 33 | drawMark(&painter); 34 | drawTextRect(&painter); 35 | drawUnderRect(&painter); 36 | drawGauge(&painter); 37 | 38 | } 39 | 40 | void myGauge6::drawVariables(QPainter *painter) 41 | { 42 | m_leftSpace=(qreal)width()/7; 43 | m_topSpace=(qreal)height()/10; 44 | m_long=m_leftSpace/4; 45 | m_ok=m_long/3; 46 | m_short=m_long/2; 47 | } 48 | 49 | void myGauge6::drawBackground(QPainter *painter) 50 | { 51 | painter->save(); 52 | QLinearGradient bgGradient(QPointF(0,0),QPointF(0,height())); 53 | bgGradient.setColorAt(0.0,QColor(30,30,30)); 54 | bgGradient.setColorAt(0.1,QColor(30,30,30)); 55 | bgGradient.setColorAt(0.9,QColor(30,30,30)); 56 | bgGradient.setColorAt(1.0,QColor(30,30,30)); 57 | 58 | painter->setPen(Qt::NoPen); 59 | painter->setBrush(bgGradient); 60 | painter->drawRect(rect()); 61 | 62 | painter->restore(); 63 | } 64 | 65 | void myGauge6::SetGraphColor(QColor c) 66 | { 67 | this->graphcolor=c; 68 | } 69 | 70 | void myGauge6::drawGauge(QPainter *painter) 71 | { 72 | painter->save(); 73 | 74 | QRectF gaugeRect(m_gaugeTopLeftPot,m_gaugeBottomRightPot); 75 | QLinearGradient gaugeGradient(gaugeRect.topLeft(),gaugeRect.bottomLeft()); 76 | gaugeGradient.setColorAt(0.0,graphcolor); 77 | painter->setPen(Qt::NoPen); 78 | painter->setBrush(gaugeGradient); 79 | 80 | qreal length=gaugeRect.height(); 81 | qreal increment=(qreal)length/(m_nMax-m_nMin); 82 | 83 | qreal value=m_currentValue*increment; 84 | 85 | QPointF graphTopLeftPot(m_gaugeTopLeftPot.x(),m_gaugeBottomRightPot.y()-value); 86 | QRectF graphRect(graphTopLeftPot,m_gaugeBottomRightPot); 87 | painter->drawRect(graphRect); 88 | painter->restore(); 89 | } 90 | 91 | void myGauge6::drawMark(QPainter *painter) 92 | { 93 | painter->save(); 94 | // draw vertical line 95 | painter->setPen(Qt::white); 96 | QPointF topPot(width()-2*m_leftSpace,m_topSpace); 97 | m_markX=topPot.x(); 98 | m_markY=topPot.y(); 99 | QPointF bottomPot(width()-2*m_leftSpace,height()-2*m_topSpace); 100 | m_markBottom=bottomPot.y(); 101 | painter->drawLine(topPot,bottomPot); 102 | int length=m_nMax-m_nMin; 103 | qreal increment=(qreal)(height()-3*m_topSpace)/length; 104 | 105 | int value=m_nMax; 106 | QString strValue; 107 | qreal startY=topPot.y(); 108 | 109 | for(int i=value;i>=m_nMin;i--) 110 | { 111 | if(i%10==0) 112 | { 113 | strValue=tr("%1").arg(value); 114 | //qreal textWidth=fontMetrics().width(strValue); 115 | qreal textHeight=fontMetrics().height(); 116 | 117 | QPointF rightPot(topPot.x(),startY); 118 | QPointF leftPot(topPot.x()-m_long,startY); 119 | painter->drawLine(rightPot,leftPot); 120 | 121 | QPointF textPot(topPot.x()+m_long,startY+textHeight/2); 122 | painter->drawText(textPot,strValue); 123 | value-=10; 124 | } 125 | else if(i%5==0) 126 | { 127 | QPointF rightPot(topPot.x(),startY); 128 | QPointF leftPot(topPot.x()-m_ok,startY); 129 | painter->drawLine(rightPot,leftPot); 130 | } 131 | else 132 | { 133 | QPointF rightPot(topPot.x(),startY); 134 | QPointF leftPot(topPot.x()-m_short,startY); 135 | painter->drawLine(rightPot,leftPot); 136 | } 137 | startY+=increment; 138 | } 139 | painter->restore(); 140 | } 141 | 142 | void myGauge6::drawUnderRect(QPainter *painter) 143 | { 144 | painter->save(); 145 | QLinearGradient rectGradient(m_underRect.topLeft(),m_underRect.topRight()); 146 | rectGradient.setColorAt(0.0,QColor(150,150,150)); 147 | rectGradient.setColorAt(0.1,QColor(100,100,100)); 148 | rectGradient.setColorAt(0.9,QColor(100,100,100)); 149 | rectGradient.setColorAt(1.0,QColor(150,150,150)); 150 | 151 | painter->setPen(Qt::NoPen); 152 | painter->setBrush(rectGradient); 153 | painter->drawRect(m_underRect); 154 | 155 | painter->restore(); 156 | } 157 | 158 | void myGauge6::drawTextRect(QPainter *painter) 159 | { 160 | painter->save(); 161 | 162 | QPointF topLeftPot(m_markX-3*m_long-m_leftSpace,m_markBottom+m_long); 163 | QPointF bottomRightPot(m_markX-2*m_long,m_markBottom+m_leftSpace); 164 | 165 | m_textRect=QRectF(topLeftPot,bottomRightPot); 166 | painter->setPen(Qt::NoPen); 167 | 168 | QLinearGradient textRectGradient(m_textRect.topLeft(),m_textRect.bottomLeft()); 169 | textRectGradient.setColorAt(0.0,QColor(180,180,180)); 170 | textRectGradient.setColorAt(0.2,QColor(100,100,100)); 171 | textRectGradient.setColorAt(0.8,QColor(100,100,100)); 172 | textRectGradient.setColorAt(1.0,QColor(180,180,180)); 173 | painter->setBrush(textRectGradient); 174 | painter->drawRect(m_textRect); 175 | 176 | painter->save(); 177 | painter->setPen(Qt::green); 178 | painter->drawText(m_textRect,Qt::AlignVCenter|Qt::AlignHCenter,tr("%1").arg(m_currentValue)); 179 | painter->restore(); 180 | 181 | m_underRect=QRectF(QPointF(topLeftPot.x(),m_markY),QPointF(bottomRightPot.x(),m_markBottom)); 182 | 183 | m_gaugeTopLeftPot=QPointF(m_textRect.topLeft().x(),m_markY); 184 | m_gaugeBottomRightPot=QPointF(m_textRect.bottomRight().x(),m_markBottom); 185 | 186 | painter->restore(); 187 | } 188 | 189 | void myGauge6::UpdateGraph() 190 | { 191 | if(m_bReverse) 192 | { 193 | m_currentValue-=0.5; 194 | if(m_currentValue<=m_value) 195 | { 196 | updateTimer->stop(); 197 | } 198 | } 199 | else 200 | { 201 | m_currentValue+=0.5; 202 | if(m_currentValue>=m_value) 203 | { 204 | updateTimer->stop(); 205 | } 206 | } 207 | update(); 208 | } 209 | 210 | void myGauge6::setValue(qreal value) 211 | { 212 | if(value>m_value) 213 | { 214 | m_bReverse=false; 215 | m_value=value; 216 | } 217 | else if(valuestart(); 234 | } 235 | 236 | void myGauge6::setUseAntialiasing(bool use) 237 | { 238 | m_bUseAntialiasing=use; 239 | update(); 240 | } 241 | 242 | 243 | void myGauge6::setRange(int min, int max) 244 | { 245 | m_nMin=min; 246 | m_nMax=max; 247 | update(); 248 | } 249 | 250 | void myGauge6::setAnimating(bool animate) 251 | { 252 | m_bAnimate=animate; 253 | update(); 254 | } 255 | -------------------------------------------------------------------------------- /mygauge6.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGAUGE6_H 2 | #define MYGAUGE6_H 3 | 4 | #include 5 | #include 6 | 7 | class myGauge6 : public QWidget 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit myGauge6(QWidget *parent = 0); 12 | void SetGraphColor(QColor c); 13 | 14 | public slots: 15 | void setValue(qreal value); 16 | void setRange(int min,int max); 17 | void setUseAntialiasing(bool use); 18 | void setAnimating(bool animate); 19 | 20 | protected: 21 | void paintEvent(QPaintEvent *); 22 | 23 | QSize sizeHint() const 24 | { 25 | return QSize(200,350); 26 | } 27 | 28 | QSize minimumSizeHint() const 29 | { 30 | return QSize(120,70); 31 | } 32 | 33 | private: 34 | void drawVariables(QPainter* painter); 35 | void drawBackground(QPainter* painter); 36 | void drawTextRect(QPainter* painter); 37 | void drawGauge(QPainter* painter); 38 | void drawMark(QPainter* painter); 39 | void drawUnderRect(QPainter* painter); 40 | 41 | 42 | private: 43 | qreal m_currentValue; 44 | qreal m_value; 45 | qreal m_leftSpace; 46 | qreal m_topSpace; 47 | qreal m_long; 48 | qreal m_ok; 49 | qreal m_short; 50 | qreal m_markX; 51 | qreal m_markY; 52 | qreal m_markBottom; 53 | 54 | int m_nMin; 55 | int m_nMax; 56 | bool m_bReverse; 57 | bool m_bAnimate; 58 | bool m_bUseAntialiasing; 59 | QTimer* updateTimer; 60 | QRectF m_textRect; 61 | QRectF m_underRect; 62 | QPointF m_gaugeTopLeftPot; 63 | QPointF m_gaugeBottomRightPot; 64 | QColor graphcolor; 65 | 66 | private slots: 67 | void UpdateGraph(); 68 | 69 | }; 70 | 71 | #endif // MYGAUGE6_H 72 | -------------------------------------------------------------------------------- /mygauge7.cpp: -------------------------------------------------------------------------------- 1 | #include "mygauge7.h" 2 | 3 | myGauge7::myGauge7(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | this->initVariables(); 7 | } 8 | 9 | void myGauge7::initVariables() 10 | { 11 | m_markClr=Qt::white; 12 | graphcolor=Qt::darkGreen; 13 | m_value=0; 14 | m_currentValue=0; 15 | m_bucketWidth=width()/3; 16 | m_markSpace=(qreal)width()/4; 17 | m_topSpace=(qreal)height()/15; 18 | updateTimer=new QTimer(this); 19 | updateTimer->setInterval(10); 20 | connect(updateTimer,SIGNAL(timeout()),this,SLOT(UpdateGraph())); 21 | m_bReverse=false; 22 | } 23 | 24 | void myGauge7::paintEvent(QPaintEvent *e) 25 | { 26 | QPainter painter; 27 | painter.begin(this); 28 | painter.setRenderHints(QPainter::TextAntialiasing|QPainter::Antialiasing); 29 | resetDrawingVariables(&painter); 30 | drawBackground(&painter); 31 | drawMark(&painter); 32 | drawBucket(&painter); 33 | drawGraph(&painter); 34 | 35 | painter.end(); 36 | } 37 | 38 | void myGauge7::drawBackground(QPainter *painter) 39 | { 40 | painter->save(); 41 | 42 | QLinearGradient bgGradient(QPointF(0,0),QPointF(0,height())); 43 | bgGradient.setColorAt(0.0,QColor(200,200,200)); 44 | bgGradient.setColorAt(0.2,QColor(100,100,100)); 45 | bgGradient.setColorAt(0.9,QColor(100,100,100)); 46 | bgGradient.setColorAt(1.0,QColor(200,200,200)); 47 | 48 | painter->setBrush(bgGradient); 49 | painter->drawRect(rect()); 50 | painter->restore(); 51 | } 52 | void myGauge7::drawMark(QPainter *painter) 53 | { 54 | painter->save(); 55 | 56 | qreal initY=m_topSpace; 57 | qreal initX=m_markSpace; 58 | qreal length=height()-2*m_topSpace; 59 | qreal increment=length/100; 60 | painter->setPen(m_markClr); 61 | 62 | // draw vertical line 63 | QPointF topPot(m_markSpace,m_topSpace); 64 | QPointF bottomPot(m_markSpace,height()-m_topSpace); 65 | painter->drawLine(topPot,bottomPot); 66 | 67 | int vvalue=100; 68 | QString strValue; 69 | for(int i=0;i<=100;i++) 70 | { 71 | if(i%10==0) 72 | { 73 | QPointF leftPot(initX+LONG3,initY); 74 | QPointF rightPot(initX,initY); 75 | painter->drawLine(leftPot,rightPot); 76 | 77 | strValue=tr("%1").arg(vvalue); 78 | qreal fontWidth=painter->fontMetrics().width(strValue); 79 | qreal fontHeight=painter->fontMetrics().height(); 80 | 81 | QPointF textPot(initX-fontWidth-5,initY+fontHeight/2); 82 | painter->drawText(textPot,strValue); 83 | 84 | vvalue-=10; 85 | 86 | } 87 | else if(i%5==0 && i%10!=0) 88 | { 89 | QPointF leftPot(initX+OK3,initY); 90 | QPointF rightPot(initX,initY); 91 | painter->drawLine(leftPot,rightPot); 92 | } 93 | else 94 | { 95 | QPointF leftPot(initX+SHORT3,initY); 96 | QPointF rightPot(initX,initY); 97 | painter->drawLine(leftPot,rightPot); 98 | } 99 | initY+=increment; 100 | } 101 | painter->restore(); 102 | } 103 | 104 | void myGauge7::SetGraphColor(QColor c) 105 | { 106 | this->graphcolor=c; 107 | } 108 | 109 | 110 | void myGauge7::drawGraph(QPainter *painter) 111 | { 112 | painter->save(); 113 | qreal bucketHeight=m_bucketRect.height(); 114 | qreal increment=(qreal)bucketHeight/100; 115 | 116 | QPointF bottomRightPot(m_bucketRect.bottomRight()); 117 | qreal currentY=m_currentValue*increment; 118 | 119 | QPointF topLeftPot(m_bucketRect.topLeft().x(),m_bucketRect.bottomLeft().y()-currentY); 120 | QRectF graphRect(topLeftPot,bottomRightPot); 121 | painter->setPen(Qt::NoPen); 122 | 123 | QLinearGradient graphGradient(graphRect.topLeft(),graphRect.topRight()); 124 | painter->setOpacity(0.7); 125 | 126 | graphGradient.setColorAt(0.0,this->graphcolor); 127 | 128 | painter->setBrush(graphGradient); 129 | painter->drawRect(graphRect); 130 | painter->restore(); 131 | } 132 | 133 | void myGauge7::drawBucket(QPainter *painter) 134 | { 135 | painter->save(); 136 | 137 | QPointF bucketTopLeftPot(m_markSpace+MARK_BUCKET_SPACE3,m_topSpace); 138 | QPointF bucketBottomRightPot(m_markSpace+MARK_BUCKET_SPACE3+m_bucketWidth,height()-m_topSpace); 139 | QRectF bucketRect(bucketTopLeftPot,bucketBottomRightPot); 140 | m_bucketRect=bucketRect; 141 | 142 | QLinearGradient bucketGradient(bucketRect.topLeft(),bucketRect.topRight()); 143 | bucketGradient.setColorAt(0.0,QColor(100,100,100)); 144 | bucketGradient.setColorAt(0.6,QColor(200,200,200)); 145 | bucketGradient.setColorAt(1.0,QColor(150,150,150)); 146 | 147 | painter->setPen(Qt::NoPen); 148 | painter->setBrush(bucketGradient); 149 | painter->drawRect(bucketRect); 150 | painter->restore(); 151 | } 152 | 153 | void myGauge7::resetDrawingVariables(QPainter *painter) 154 | { 155 | m_markSpace=(qreal)width()/4; 156 | m_bucketWidth=(qreal)width()/3; 157 | m_topSpace=(qreal)height()/15; 158 | } 159 | 160 | 161 | void myGauge7::setValue(qreal value) 162 | { 163 | if(value>m_value) 164 | { 165 | m_bReverse=false; 166 | } 167 | else if(valuestart(); 177 | } 178 | 179 | void myGauge7::setMarkColor(const QColor &clr) 180 | { 181 | m_markClr=clr; 182 | update(); 183 | } 184 | 185 | 186 | void myGauge7::UpdateGraph() 187 | { 188 | if(!m_bReverse) 189 | { 190 | m_currentValue+=0.5; 191 | if(m_currentValue>=m_value) 192 | { 193 | updateTimer->stop(); 194 | } 195 | } 196 | else 197 | { 198 | m_currentValue-=0.5; 199 | 200 | if(m_currentValue<=m_value) 201 | { 202 | updateTimer->stop(); 203 | } 204 | } 205 | update(); 206 | } 207 | -------------------------------------------------------------------------------- /mygauge7.h: -------------------------------------------------------------------------------- 1 | #ifndef MYGAUGE7_H 2 | #define MYGAUGE7_H 3 | 4 | #define LONG3 7 5 | #define OK3 5 6 | #define SHORT3 3 7 | #define MARK_BUCKET_SPACE3 20 8 | 9 | #include 10 | #include 11 | 12 | class myGauge7 : public QWidget 13 | { 14 | Q_OBJECT 15 | public: 16 | explicit myGauge7(QWidget *parent = 0); 17 | 18 | void SetGraphColor(QColor c); 19 | 20 | protected: 21 | void paintEvent(QPaintEvent *); 22 | QSize sizeHint() const 23 | { 24 | return QSize(300,300); 25 | } 26 | 27 | QSize minimumSizeHint() const 28 | { 29 | return QSize(200,200); 30 | } 31 | 32 | private: 33 | void drawMark(QPainter* painter); 34 | void drawBucket(QPainter* painter); 35 | void drawBackground(QPainter* painter); 36 | void drawGraph(QPainter* painter); 37 | void resetDrawingVariables(QPainter* painter); 38 | 39 | private: 40 | qreal m_value; 41 | qreal m_currentValue; 42 | qreal m_bucketWidth; 43 | QTimer* updateTimer; 44 | bool m_bReverse; 45 | qreal m_markSpace; 46 | qreal m_topSpace; 47 | QColor m_markClr; 48 | QColor graphcolor; 49 | QRectF m_bucketRect; 50 | 51 | private: 52 | void initVariables(); 53 | 54 | private slots: 55 | void UpdateGraph(); 56 | 57 | public slots: 58 | void setValue(qreal value); 59 | void setMarkColor(const QColor& clr); 60 | 61 | }; 62 | 63 | #endif // MYGAUGE7_H 64 | -------------------------------------------------------------------------------- /myindicator.cpp: -------------------------------------------------------------------------------- 1 | #include "myindicator.h" 2 | 3 | myIndicator::myIndicator(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | this->initVariables(); 7 | } 8 | 9 | void myIndicator::initVariables() 10 | { 11 | for(int i = 0;i < INDICATOR_COUNT; i++) 12 | { 13 | lcd[i]=new myLCDNumber(this); 14 | } 15 | m_value=0; 16 | m_currValue=0; 17 | m_bReverse=false; 18 | 19 | m_topSpace=0; 20 | m_leftSpace=0; 21 | m_lcdWidth=0; 22 | m_lcdHeight=0; 23 | forecolor=Qt::darkGreen; 24 | 25 | updateTimer=new QTimer(this); 26 | updateTimer->setInterval(10); 27 | connect(updateTimer,SIGNAL(timeout()),this,SLOT(UpdateData())); 28 | } 29 | 30 | 31 | void myIndicator::paintEvent(QPaintEvent *) 32 | { 33 | QPainter painter(this); 34 | painter.setRenderHints(QPainter::Antialiasing); 35 | drawVariables(&painter); 36 | drawBackground(&painter); 37 | drawLCDNums(&painter); 38 | drawRect(&painter); 39 | drawGraph(&painter); 40 | drawBars(&painter); 41 | } 42 | 43 | void myIndicator::drawVariables(QPainter *painter) 44 | { 45 | m_leftSpace=width()/10; 46 | m_topSpace=height()/25; 47 | m_lcdWidth=(width()-(width()/5))/3; 48 | m_lcdHeight=height()/12; 49 | 50 | QPointF topLeftPot(m_leftSpace,m_topSpace); 51 | m_rectTopLeft=topLeftPot; 52 | QPointF bottomRightPot(width()-m_leftSpace,height()-2*m_topSpace-m_lcdHeight); 53 | m_rectBottomRight=bottomRightPot; 54 | } 55 | 56 | void myIndicator::drawBackground(QPainter* painter) 57 | { 58 | painter->save(); 59 | 60 | QLinearGradient bgGradient(QPointF(0,0),QPointF(width(),0)); 61 | bgGradient.setColorAt(0.0,QColor(30,30,30)); 62 | bgGradient.setColorAt(0.1,QColor(30,30,30)); 63 | bgGradient.setColorAt(0.9,QColor(30,30,30)); 64 | bgGradient.setColorAt(1.0,QColor(30,30,30)); 65 | 66 | painter->setPen(Qt::NoPen); 67 | painter->setBrush(bgGradient); 68 | painter->drawRect(rect()); 69 | painter->restore(); 70 | } 71 | 72 | void myIndicator::drawLCDNums(QPainter* painter) 73 | { 74 | painter->save(); 75 | 76 | int topLeftX=width()/10; 77 | int topLeftY=height()-m_topSpace-m_lcdHeight; 78 | QPointF topLeft(topLeftX,topLeftY); 79 | 80 | for(int i=0;iresize(m_lcdWidth,m_lcdHeight); 83 | lcd[i]->move(QPoint(topLeftX,topLeftY)); 84 | topLeftX+=m_lcdWidth; 85 | } 86 | painter->restore(); 87 | } 88 | 89 | void myIndicator::drawRect(QPainter* painter) 90 | { 91 | painter->save(); 92 | QRectF theRect(m_rectTopLeft,m_rectBottomRight); 93 | painter->setPen(QPen(INDICATOR_FRAME_COLOR,INDICATOR_PEN_WIDTH)); 94 | painter->setBrush(QBrush(INDICATOR_FRAME_COLOR,Qt::Dense4Pattern)); 95 | painter->drawRect(theRect); 96 | painter->restore(); 97 | } 98 | 99 | void myIndicator::SetForeColor(QColor c) 100 | { 101 | this->forecolor=c; 102 | } 103 | 104 | void myIndicator::drawGraph(QPainter *painter) 105 | { 106 | painter->save(); 107 | painter->setPen(QPen(Qt::transparent,INDICATOR_PEN_WIDTH)); 108 | 109 | QLinearGradient graphGradient(m_rectTopLeft,QPointF(m_rectTopLeft.x(),m_rectBottomRight.y())); 110 | graphGradient.setColorAt(0.0,forecolor); 111 | painter->setBrush(graphGradient); 112 | 113 | qreal dY=(qreal)(m_rectTopLeft.y()-m_rectBottomRight.y())/100; 114 | 115 | qreal yValue=dY*m_currValue; 116 | 117 | QPointF topLeftPot(m_rectTopLeft.x()+INDICATOR_PEN_WIDTH,m_rectBottomRight.y()+yValue); 118 | QPointF bottomRightPot(m_rectBottomRight.x()-INDICATOR_PEN_WIDTH,m_rectBottomRight.y()); 119 | QRectF graphRect(topLeftPot,bottomRightPot); 120 | painter->drawRect(graphRect); 121 | 122 | QString strCurrValue; 123 | strCurrValue=tr("%1").arg((m_currValue)); 124 | if(m_currValue<10) 125 | { 126 | lcd[0]->setNumber(0); 127 | lcd[1]->setNumber(0); 128 | lcd[2]->setNumber(m_currValue); 129 | } 130 | else if(m_currValue < 100 && m_currValue >= 10) 131 | { 132 | lcd[0]->setNumber(0); 133 | lcd[1]->setNumber(strCurrValue.at(0).digitValue()); 134 | lcd[2]->setNumber(strCurrValue.at(1).digitValue()); 135 | } 136 | else 137 | { 138 | lcd[0]->setNumber(1); 139 | lcd[1]->setNumber(0); 140 | lcd[2]->setNumber(0); 141 | } 142 | painter->restore(); 143 | } 144 | 145 | void myIndicator::drawBars(QPainter* painter) 146 | { 147 | painter->save(); 148 | painter->setPen(QPen(INDICATOR_FRAME_COLOR,INDICATOR_PEN_WIDTH)); 149 | 150 | qreal left=m_rectTopLeft.x(); 151 | qreal right=m_rectBottomRight.x(); 152 | 153 | qreal top=m_rectTopLeft.y(); 154 | qreal bottom=m_rectBottomRight.y(); 155 | qreal increment=(bottom-top)/10; 156 | 157 | while(topdrawLine(leftPot,rightPot); 162 | top+=increment; 163 | } 164 | painter->restore(); 165 | 166 | } 167 | 168 | void myIndicator::UpdateData() 169 | { 170 | if(m_bReverse) 171 | { 172 | m_currValue--; 173 | 174 | if(m_currValue<=m_value) 175 | { 176 | updateTimer->stop(); 177 | } 178 | } 179 | else 180 | { 181 | m_currValue++; 182 | 183 | if(m_currValue>=m_value) 184 | { 185 | updateTimer->stop(); 186 | } 187 | } 188 | update(); 189 | } 190 | 191 | 192 | void myIndicator::setValue(int value) 193 | { 194 | if(m_value==value) 195 | { 196 | return ; 197 | } 198 | 199 | if(value>m_value) 200 | { 201 | m_bReverse=false; 202 | }else{ 203 | m_bReverse=true; 204 | } 205 | m_value=value; 206 | 207 | updateTimer->start(); 208 | } 209 | -------------------------------------------------------------------------------- /myindicator.h: -------------------------------------------------------------------------------- 1 | #ifndef MYINDICATOR_H 2 | #define MYINDICATOR_H 3 | 4 | #include 5 | #include 6 | #include "mylcdnumber.h" 7 | 8 | const int INDICATOR_COUNT = 3; 9 | const int INDICATOR_PEN_WIDTH = 2; 10 | const QColor INDICATOR_FRAME_COLOR = QColor(255,170,0); 11 | const QColor INDICATOR_GRAPH_COLOR = QColor(85,255,0); 12 | 13 | class myIndicator : public QWidget 14 | { 15 | Q_OBJECT 16 | public: 17 | explicit myIndicator(QWidget *parent = 0); 18 | void SetForeColor(QColor c); 19 | 20 | protected: 21 | QSize sizeHint() const 22 | { 23 | return QSize(70,300); 24 | } 25 | 26 | QSize minimumSizeHint() const 27 | { 28 | return QSize(80,200); 29 | } 30 | 31 | void paintEvent(QPaintEvent *); 32 | 33 | private: 34 | void drawVariables(QPainter* painter); 35 | void drawBackground(QPainter* painter); 36 | void drawLCDNums(QPainter* painter); 37 | void drawRect(QPainter* painter); 38 | void drawBars(QPainter* painter); 39 | void drawGraph(QPainter* painter); 40 | 41 | private: 42 | void initVariables(); 43 | 44 | private: 45 | myLCDNumber* lcd[INDICATOR_COUNT]; 46 | qreal m_leftSpace; 47 | qreal m_topSpace; 48 | qreal m_lcdWidth; 49 | qreal m_lcdHeight; 50 | int m_value; 51 | int m_currValue; 52 | bool m_bReverse; 53 | 54 | QTimer* updateTimer; 55 | QPointF m_rectTopLeft; 56 | QPointF m_rectBottomRight; 57 | QColor forecolor; 58 | 59 | private slots: 60 | void UpdateData(); 61 | 62 | public slots: 63 | void setValue(int value); 64 | 65 | }; 66 | 67 | #endif // MYINDICATOR_H 68 | -------------------------------------------------------------------------------- /mylcdnumber.h: -------------------------------------------------------------------------------- 1 | #ifndef MYLCDNUMBER_H 2 | #define MYLCDNUMBER_H 3 | 4 | #define LCDNUMBER_SPACE 2 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | class myLCDNumber : public QWidget 11 | { 12 | Q_OBJECT 13 | public: 14 | explicit myLCDNumber(QWidget *parent = 0); 15 | 16 | signals: 17 | 18 | public slots: 19 | void setNumber(int num); 20 | 21 | protected: 22 | 23 | QSize sizeHint() const 24 | { 25 | return QSize(20,30); 26 | } 27 | 28 | QSize minimumSizeHint() const 29 | { 30 | return QSize(10,15); 31 | } 32 | void paintEvent(QPaintEvent *); 33 | 34 | private: 35 | void drawVariables(QPainter* painter); 36 | void drawBackground(QPainter* painter); 37 | void drawNumber(QPainter* painter); 38 | 39 | private: 40 | int m_nNum; 41 | qreal m_min; 42 | qreal m_rectWidth; 43 | QPointF m_center; 44 | void initVariables(); 45 | 46 | }; 47 | 48 | #endif // MYLCDNUMBER_H 49 | -------------------------------------------------------------------------------- /mynewled.cpp: -------------------------------------------------------------------------------- 1 | #include "mynewled.h" 2 | 3 | #define DRAW_LED(NumberObj) if(NumberObj->hasThisPair(hIndex,vIndex)) \ 4 | { \ 5 | goto DoPainting; \ 6 | }else{ \ 7 | goto DoNothing; \ 8 | } 9 | 10 | #define ADD_COOR(Obj,X,Y) Obj->addPair(X,Y); 11 | 12 | myNewLed::myNewLed(QWidget *parent) : 13 | QWidget(parent) 14 | { 15 | this->initVariables(); 16 | } 17 | 18 | void myNewLed::initVariables() 19 | { 20 | 21 | this->myledColor1=QColor(55,142,234); 22 | this->myledColor2=QColor(48,125,207); 23 | 24 | m_nNum = 0; 25 | 26 | zero = new NumberObj(); 27 | one = new NumberObj(); 28 | two = new NumberObj(); 29 | three = new NumberObj(); 30 | four = new NumberObj(); 31 | five = new NumberObj(); 32 | six = new NumberObj(); 33 | seven = new NumberObj(); 34 | eight = new NumberObj(); 35 | nine = new NumberObj(); 36 | 37 | /// stuff zero 38 | ADD_COOR(zero,2,1); 39 | ADD_COOR(zero,3,1); 40 | ADD_COOR(zero,4,1); 41 | ADD_COOR(zero,5,1); 42 | ADD_COOR(zero,6,1); 43 | ADD_COOR(zero,2,2); 44 | ADD_COOR(zero,2,3); 45 | ADD_COOR(zero,2,4); 46 | ADD_COOR(zero,2,5); 47 | ADD_COOR(zero,2,6); 48 | ADD_COOR(zero,2,7); 49 | ADD_COOR(zero,3,7); 50 | ADD_COOR(zero,4,7); 51 | ADD_COOR(zero,5,7); 52 | ADD_COOR(zero,6,7); 53 | ADD_COOR(zero,6,2); 54 | ADD_COOR(zero,6,3); 55 | ADD_COOR(zero,6,4); 56 | ADD_COOR(zero,6,5); 57 | ADD_COOR(zero,6,6); 58 | 59 | 60 | /// stuff one 61 | // ADD_COOR(one,2,3); 62 | ADD_COOR(one,3,2); 63 | ADD_COOR(one,4,1); 64 | ADD_COOR(one,4,2); 65 | ADD_COOR(one,4,3); 66 | ADD_COOR(one,4,7); 67 | ADD_COOR(one,4,4); 68 | ADD_COOR(one,4,5); 69 | ADD_COOR(one,4,6); 70 | // ADD_COOR(one,2,7); 71 | ADD_COOR(one,3,7); 72 | // ADD_COOR(one,6,7); 73 | ADD_COOR(one,5,7); 74 | 75 | /// stuff two 76 | ADD_COOR(two,2,1); 77 | ADD_COOR(two,3,1); 78 | ADD_COOR(two,4,1); 79 | ADD_COOR(two,5,1); 80 | ADD_COOR(two,6,1); 81 | ADD_COOR(two,6,2); 82 | ADD_COOR(two,6,3); 83 | ADD_COOR(two,6,4); 84 | ADD_COOR(two,2,5); 85 | ADD_COOR(two,2,6); 86 | ADD_COOR(two,2,7); 87 | ADD_COOR(two,3,7); 88 | ADD_COOR(two,4,7); 89 | ADD_COOR(two,5,7); 90 | ADD_COOR(two,6,7); 91 | ADD_COOR(two,5,4); 92 | ADD_COOR(two,4,4); 93 | ADD_COOR(two,3,4); 94 | ADD_COOR(two,2,4); 95 | 96 | /// stuff three 97 | ADD_COOR(three,2,1); 98 | ADD_COOR(three,3,1); 99 | ADD_COOR(three,4,1); 100 | ADD_COOR(three,5,1); 101 | ADD_COOR(three,6,1); 102 | ADD_COOR(three,6,2); 103 | ADD_COOR(three,6,3); 104 | ADD_COOR(three,6,4); 105 | ADD_COOR(three,6,5); 106 | ADD_COOR(three,6,6); 107 | ADD_COOR(three,2,7); 108 | ADD_COOR(three,3,7); 109 | ADD_COOR(three,4,7); 110 | ADD_COOR(three,5,7); 111 | ADD_COOR(three,6,7); 112 | ADD_COOR(three,2,4); 113 | ADD_COOR(three,3,4); 114 | ADD_COOR(three,4,4); 115 | ADD_COOR(three,5,4); 116 | 117 | 118 | /// stuff four 119 | ADD_COOR(four,4,1); 120 | ADD_COOR(four,3,2); 121 | ADD_COOR(four,2,3); 122 | ADD_COOR(four,2,4); 123 | ADD_COOR(four,3,4); 124 | ADD_COOR(four,4,4); 125 | ADD_COOR(four,5,4); 126 | ADD_COOR(four,6,4); 127 | ADD_COOR(four,4,2); 128 | ADD_COOR(four,4,3); 129 | ADD_COOR(four,4,4); 130 | ADD_COOR(four,4,5); 131 | ADD_COOR(four,4,6); 132 | ADD_COOR(four,4,7); 133 | ADD_COOR(four,1,4); 134 | 135 | /// stuff five 136 | ADD_COOR(five,6,1); 137 | ADD_COOR(five,5,1); 138 | ADD_COOR(five,4,1); 139 | ADD_COOR(five,3,1); 140 | ADD_COOR(five,2,1); 141 | ADD_COOR(five,2,2); 142 | ADD_COOR(five,2,3); 143 | ADD_COOR(five,2,4); 144 | ADD_COOR(five,3,4); 145 | ADD_COOR(five,4,4); 146 | ADD_COOR(five,5,4); 147 | ADD_COOR(five,6,4); 148 | ADD_COOR(five,6,6); 149 | ADD_COOR(five,6,7); 150 | ADD_COOR(five,5,7); 151 | ADD_COOR(five,4,7); 152 | ADD_COOR(five,3,7); 153 | ADD_COOR(five,2,7); 154 | ADD_COOR(five,6,5); 155 | 156 | 157 | /// stuff six 158 | ADD_COOR(six,6,1); 159 | ADD_COOR(six,5,1); 160 | ADD_COOR(six,4,1); 161 | ADD_COOR(six,3,1); 162 | ADD_COOR(six,2,1); 163 | ADD_COOR(six,2,2); 164 | ADD_COOR(six,2,3); 165 | ADD_COOR(six,2,4); 166 | ADD_COOR(six,3,4); 167 | ADD_COOR(six,4,4); 168 | ADD_COOR(six,5,4); 169 | ADD_COOR(six,6,4); 170 | ADD_COOR(six,6,6); 171 | ADD_COOR(six,6,7); 172 | ADD_COOR(six,5,7); 173 | ADD_COOR(six,4,7); 174 | ADD_COOR(six,3,7); 175 | ADD_COOR(six,2,7); 176 | ADD_COOR(six,6,5); 177 | ADD_COOR(six,2,5); 178 | ADD_COOR(six,2,2); 179 | ADD_COOR(six,2,6); 180 | 181 | 182 | /// stuff seven 183 | ADD_COOR(seven,2,1); 184 | ADD_COOR(seven,3,1); 185 | ADD_COOR(seven,4,1); 186 | ADD_COOR(seven,5,1); 187 | ADD_COOR(seven,6,1); 188 | ADD_COOR(seven,6,2); 189 | ADD_COOR(seven,5,3); 190 | ADD_COOR(seven,4,4); 191 | ADD_COOR(seven,3,5); 192 | ADD_COOR(seven,2,6); 193 | ADD_COOR(seven,2,7); 194 | 195 | /// stuff eight 196 | ADD_COOR(eight,6,1); 197 | ADD_COOR(eight,5,1); 198 | ADD_COOR(eight,4,1); 199 | ADD_COOR(eight,3,1); 200 | ADD_COOR(eight,2,1); 201 | ADD_COOR(eight,2,2); 202 | ADD_COOR(eight,2,3); 203 | ADD_COOR(eight,2,4); 204 | ADD_COOR(eight,3,4); 205 | ADD_COOR(eight,4,4); 206 | ADD_COOR(eight,5,4); 207 | ADD_COOR(eight,6,4); 208 | ADD_COOR(eight,6,6); 209 | ADD_COOR(eight,6,7); 210 | ADD_COOR(eight,5,7); 211 | ADD_COOR(eight,4,7); 212 | ADD_COOR(eight,3,7); 213 | ADD_COOR(eight,2,7); 214 | ADD_COOR(eight,6,5); 215 | ADD_COOR(eight,2,2); 216 | ADD_COOR(eight,6,2); 217 | ADD_COOR(eight,6,3); 218 | ADD_COOR(eight,2,5); 219 | ADD_COOR(eight,2,6); 220 | 221 | /// stuff nine 222 | ADD_COOR(nine,6,1); 223 | ADD_COOR(nine,5,1); 224 | ADD_COOR(nine,4,1); 225 | ADD_COOR(nine,3,1); 226 | ADD_COOR(nine,2,1); 227 | ADD_COOR(nine,2,2); 228 | ADD_COOR(nine,2,3); 229 | ADD_COOR(nine,2,4); 230 | ADD_COOR(nine,3,4); 231 | ADD_COOR(nine,4,4); 232 | ADD_COOR(nine,5,4); 233 | ADD_COOR(nine,6,4); 234 | ADD_COOR(nine,6,6); 235 | ADD_COOR(nine,6,7); 236 | ADD_COOR(nine,5,7); 237 | ADD_COOR(nine,4,7); 238 | ADD_COOR(nine,3,7); 239 | ADD_COOR(nine,2,7); 240 | ADD_COOR(nine,6,5); 241 | ADD_COOR(nine,2,2); 242 | ADD_COOR(nine,6,2); 243 | ADD_COOR(nine,6,3); 244 | 245 | } 246 | 247 | void myNewLed::paintEvent(QPaintEvent *) 248 | { 249 | QPainter painter(this); 250 | painter.setRenderHints(QPainter::Antialiasing); 251 | drawBg(&painter); 252 | drawLed(&painter); 253 | } 254 | 255 | void myNewLed::drawBg(QPainter *painter) 256 | { 257 | painter->save(); 258 | painter->setPen(Qt::NoPen); 259 | painter->setBrush(PYL_BG_COLOR); 260 | painter->drawRect(rect()); 261 | painter->restore(); 262 | } 263 | 264 | void myNewLed::SetmyLedColor(QColor c1, QColor c2) 265 | { 266 | this->myledColor1=c1; 267 | this->myledColor2=c2; 268 | } 269 | 270 | void myNewLed::drawLed(QPainter *painter) 271 | { 272 | qreal initX = PYL_LEFT_SPACE; 273 | qreal initY = PYL_TOP_SPACE; 274 | qreal LedWidth = (width() - 2 * PYL_LEFT_SPACE - (PYL_H_COUNT-1)*PYL_WIDGET_SPACE)/PYL_H_COUNT; 275 | qreal LedHeight = (height() - 2* PYL_TOP_SPACE -(PYL_V_COUNT-1)*PYL_WIDGET_SPACE)/PYL_V_COUNT; 276 | 277 | painter->save(); 278 | painter->setPen(Qt::NoPen); 279 | for(int hIndex = 0;hIndex < PYL_H_COUNT;hIndex++) 280 | { 281 | for(int vIndex = 0;vIndex < PYL_V_COUNT;vIndex++) 282 | { 283 | QPointF LedTopLeft(initX,initY); 284 | QPointF LedBottomRight(initX + LedWidth,initY + LedHeight); 285 | QRectF LedRect(LedTopLeft,LedBottomRight); 286 | 287 | QLinearGradient LedGradient(LedRect.topLeft(),LedRect.bottomLeft()); 288 | LedGradient.setColorAt(0.0,PYL_NON_START_COLOR); 289 | LedGradient.setColorAt(1.0,PYL_NON_END_COLOR); 290 | painter->setBrush(LedGradient); 291 | 292 | painter->drawRoundedRect(LedRect,PYL_RECT_RADIUS,PYL_RECT_RADIUS); 293 | 294 | /// increment initX 295 | initX += LedWidth + PYL_WIDGET_SPACE; 296 | } 297 | 298 | /// increment initY 299 | initY += LedHeight + PYL_WIDGET_SPACE; 300 | initX = PYL_LEFT_SPACE; 301 | } 302 | 303 | painter->restore(); 304 | 305 | // draw highlight led 306 | initX = PYL_LEFT_SPACE; 307 | initY = PYL_TOP_SPACE; 308 | qreal LedX = 0; 309 | qreal LedY = 0; 310 | 311 | painter->save(); 312 | painter->setPen(Qt::NoPen); 313 | 314 | for(int hIndex = 0;hIndex < PYL_H_COUNT;hIndex++) 315 | { 316 | for(int vIndex = 0;vIndex < PYL_V_COUNT;vIndex++) 317 | { 318 | switch(m_nNum) 319 | { 320 | case 0: 321 | DRAW_LED(zero); 322 | case 1: 323 | DRAW_LED(one); 324 | case 2: 325 | DRAW_LED(two); 326 | case 3: 327 | DRAW_LED(three); 328 | case 4: 329 | DRAW_LED(four); 330 | case 5: 331 | DRAW_LED(five); 332 | case 6: 333 | DRAW_LED(six); 334 | case 7: 335 | DRAW_LED(seven); 336 | case 8: 337 | DRAW_LED(eight); 338 | case 9: 339 | DRAW_LED(nine); 340 | 341 | 342 | } 343 | 344 | DoPainting: 345 | LedX = initX + hIndex * LedWidth + (hIndex)*PYL_WIDGET_SPACE; 346 | LedY = initY + vIndex * LedHeight + (vIndex)* PYL_WIDGET_SPACE; 347 | { 348 | QPointF LedTopLeft(LedX,LedY); 349 | QPointF LedBottomRight(LedX + LedWidth,LedY + LedHeight); 350 | QRectF LedRect(LedTopLeft,LedBottomRight); 351 | 352 | QLinearGradient LedGradient(LedRect.topLeft(),LedRect.bottomLeft()); 353 | // LedGradient.setColorAt(0.0,PYL_QUI_START_COLOR); 354 | // LedGradient.setColorAt(1.0,PYL_QUI_END_COLOR); 355 | 356 | LedGradient.setColorAt(0.0,this->myledColor1); 357 | LedGradient.setColorAt(1.0,this->myledColor2); 358 | 359 | painter->setBrush(LedGradient); 360 | painter->drawRoundedRect(LedRect,PYL_RECT_RADIUS,PYL_RECT_RADIUS); 361 | } 362 | 363 | DoNothing: 364 | ; 365 | } 366 | } 367 | 368 | painter->restore(); 369 | 370 | } 371 | 372 | void myNewLed::setNumber(int num) 373 | { 374 | m_nNum = num; 375 | update(); 376 | } 377 | -------------------------------------------------------------------------------- /mynewled.h: -------------------------------------------------------------------------------- 1 | #ifndef MYNEWLED_H 2 | #define MYNEWLED_H 3 | 4 | #define PYL_LEFT_SPACE 10 5 | #define PYL_TOP_SPACE 10 6 | 7 | #define PYL_WIDGET_SPACE 2 8 | #define PYL_RECT_RADIUS 3 9 | 10 | #define PYL_H_COUNT 9 11 | #define PYL_V_COUNT 9 12 | 13 | #define PYL_BG_COLOR QColor(45,45,45) 14 | #define PYL_NON_START_COLOR QColor(119,119,119) 15 | #define PYL_NON_END_COLOR QColor(89,89,89) 16 | 17 | #define PYL_SCHEME1 18 | #ifdef PYL_SCHEME1 19 | #define PYL_QUI_START_COLOR QColor(55,142,234) 20 | #define PYL_QUI_END_COLOR QColor(48,125,207) 21 | #endif 22 | 23 | #ifdef PYL_SCHEME2 24 | #define PYL_QUI_START_COLOR QColor(228,93,255) 25 | #define PYL_QUI_END_COLOR QColor(195,79,218) 26 | #endif 27 | 28 | #ifdef PYL_SCHEME3 29 | #define PYL_QUI_START_COLOR QColor(162,255,41) 30 | #define PYL_QUI_END_COLOR QColor(138,217,35) 31 | #endif 32 | 33 | #include 34 | 35 | 36 | #ifdef DEBUG_VAR 37 | #undef DEBUG_VAR 38 | #endif 39 | #define DEBUG_VAR(VAR) qDebug() << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << #VAR << ":" << VAR; 40 | 41 | 42 | #include 43 | #include 44 | 45 | class NumberObj : public QObject 46 | { 47 | Q_OBJECT 48 | public: 49 | NumberObj(){} 50 | 51 | void addPair(int hCoor,int vCoor) 52 | { 53 | DEBUG_VAR(hCoor); 54 | DEBUG_VAR(vCoor); 55 | m_CoorPairVec.push_back(qMakePair(hCoor,vCoor)); 56 | DEBUG_VAR(m_CoorPairVec.count()); 57 | } 58 | 59 | void clear() 60 | { 61 | m_CoorPairVec.clear(); 62 | } 63 | 64 | int pairCount() const 65 | { 66 | return m_CoorPairVec.count(); 67 | } 68 | 69 | bool hasThisPair(int hCoor,int vCoor) 70 | { 71 | int PairCnt = m_CoorPairVec.count(); 72 | for(int PairIndex = 0;PairIndex < PairCnt ; PairIndex++) 73 | { 74 | if(m_CoorPairVec.at(PairIndex).first == hCoor && 75 | m_CoorPairVec.at(PairIndex).second == vCoor) 76 | { 77 | return true; 78 | } 79 | } 80 | return false; 81 | } 82 | 83 | private: 84 | QVector< QPair > m_CoorPairVec; 85 | }; 86 | 87 | 88 | class myNewLed : public QWidget 89 | { 90 | Q_OBJECT 91 | public: 92 | explicit myNewLed(QWidget *parent = 0); 93 | 94 | public: 95 | void setNumber(int num); 96 | void SetmyLedColor(QColor c1,QColor c2); 97 | 98 | protected: 99 | void paintEvent(QPaintEvent *); 100 | 101 | QSize sizeHint() const 102 | { 103 | return QSize(300,300); 104 | } 105 | 106 | private: 107 | void drawBg(QPainter* painter); 108 | void drawLed(QPainter* painter); 109 | 110 | QColor myledColor1; 111 | QColor myledColor2; 112 | 113 | private: 114 | void initVariables(); 115 | 116 | NumberObj* zero,*one,*two,*three,*four,*five,*six,*seven,*eight,*nine; 117 | int m_nNum; 118 | 119 | }; 120 | 121 | #endif // MYNEWLED_H 122 | -------------------------------------------------------------------------------- /myphotoview.cpp: -------------------------------------------------------------------------------- 1 | #include "myphotoview.h" 2 | 3 | PYNum::PYNum(QWidget* parent):QWidget(parent) 4 | { 5 | m_opacity = 0.0; 6 | 7 | setWindowOpacity(PYN_FIN_OPACITY); 8 | 9 | m_fadeTimer = new QTimer(this); 10 | m_fadeTimer->setInterval(PYN_TIMER_INTERVAL); 11 | connect(m_fadeTimer,SIGNAL(timeout()),this,SLOT(DoFading())); 12 | 13 | m_hideTimer = new QTimer(this); 14 | m_hideTimer->setInterval(PYN_TIMER_INTERVAL); 15 | connect(m_hideTimer,SIGNAL(timeout()),this,SLOT(DoHiding())); 16 | 17 | m_holdTimer = new QTimer(this); 18 | m_holdTimer->setInterval(PYN_HOLD_DURATION); 19 | m_holdTimer->setSingleShot(true); 20 | connect(m_holdTimer,SIGNAL(timeout()),m_hideTimer,SLOT(start())); 21 | 22 | m_nTotal = 0; 23 | m_nValue = 0; 24 | 25 | setFixedHeight(PYN_MAX_HEIGHT); 26 | setWindowFlags(Qt::FramelessWindowHint); 27 | setAttribute(Qt::WA_TranslucentBackground); 28 | } 29 | 30 | void PYNum::paintEvent(QPaintEvent *) 31 | { 32 | QPainter painter(this); 33 | painter.setRenderHints(QPainter::Antialiasing|QPainter::HighQualityAntialiasing); 34 | drawBg(&painter); 35 | drawText(&painter); 36 | } 37 | 38 | void PYNum::drawBg(QPainter *painter) 39 | { 40 | painter->save(); 41 | painter->setPen(Qt::NoPen); 42 | 43 | QLinearGradient BgGradient(QPoint(0,0),QPoint(0,height())); 44 | BgGradient.setColorAt(0.0,PYN_BG_START_COLOR); 45 | BgGradient.setColorAt(1.0,PYN_BG_END_COLOR); 46 | 47 | painter->setBrush(BgGradient); 48 | painter->drawRoundedRect(rect(),PYN_RECT_RADIUS,PYN_RECT_RADIUS); 49 | painter->restore(); 50 | } 51 | 52 | void PYNum::drawText(QPainter *painter) 53 | { 54 | painter->save(); 55 | QString strText ; 56 | strText = tr("%1/%2").arg(m_nValue).arg(m_nTotal); 57 | qreal TextLength = fontMetrics().width(strText); 58 | qreal ThisWidth = TextLength * PYN_WIDTH_FACTOR; 59 | 60 | painter->setPen(PYN_TEXT_COLOR); 61 | QFont TextFont; 62 | TextFont.setBold(true); 63 | painter->setFont(TextFont); 64 | painter->drawText(rect(),strText,Qt::AlignVCenter|Qt::AlignHCenter); 65 | setFixedWidth(ThisWidth); 66 | 67 | painter->restore(); 68 | } 69 | 70 | void PYNum::showEvent(QShowEvent *) 71 | { 72 | m_opacity = 0.0; 73 | setWindowOpacity(m_opacity); 74 | m_fadeTimer->start(); 75 | } 76 | 77 | void PYNum::DoFading() 78 | { 79 | m_opacity += PYN_OPACITY_INCREMENT; 80 | 81 | if(m_opacity > PYN_FIN_OPACITY) 82 | { 83 | m_opacity = PYN_FIN_OPACITY; 84 | m_fadeTimer->stop(); 85 | m_holdTimer->start(); 86 | } 87 | setWindowOpacity(m_opacity); 88 | } 89 | 90 | void PYNum::DoHiding() 91 | { 92 | m_opacity -= PYN_OPACITY_INCREMENT; 93 | 94 | if(m_opacity < 0.0) 95 | { 96 | m_opacity = 0.0; 97 | m_hideTimer->stop(); 98 | } 99 | 100 | setWindowOpacity(m_opacity); 101 | } 102 | 103 | void PYNum::setTotal(int totalNum) 104 | { 105 | m_nTotal = totalNum; 106 | update(); 107 | } 108 | 109 | void PYNum::setValue(int value) 110 | { 111 | m_nValue = value; 112 | update(); 113 | 114 | } 115 | 116 | myPhotoView::myPhotoView(QWidget *parent) : 117 | QWidget(parent) 118 | { 119 | this->initVariables(); 120 | } 121 | 122 | void myPhotoView::initVariables() 123 | { 124 | m_currImage = 0; 125 | m_nTotal = 0; 126 | m_nCurrIndex = 0; 127 | 128 | num = new PYNum(this); 129 | connect(this,SIGNAL(sig_setTotal(int)),num,SLOT(setTotal(int))); 130 | connect(this,SIGNAL(sig_setValue(int)),num,SLOT(setValue(int))); 131 | 132 | prevButton = new QToolButton(this); 133 | nextButton = new QToolButton(this); 134 | 135 | prevButton->setIcon(QIcon(":/images/prev.png")); 136 | nextButton->setIcon(QIcon(":/images/next.png")); 137 | 138 | prevButton->setIconSize(PYP_BUTTON_SIZE); 139 | nextButton->setIconSize(PYP_BUTTON_SIZE); 140 | 141 | prevButton->setFixedSize(PYP_BUTTON_SIZE); 142 | nextButton->setFixedSize(PYP_BUTTON_SIZE); 143 | 144 | prevButton->setAutoRaise(true); 145 | nextButton->setAutoRaise(true); 146 | 147 | connect(prevButton,SIGNAL(clicked()),this,SLOT(showPrevious())); 148 | connect(nextButton,SIGNAL(clicked()),this,SLOT(showNext())); 149 | 150 | calcGeo(); 151 | 152 | #ifdef PYP_IMAGE_FADE_TIMER_INTERVAL 153 | m_imageOpacity = 0.0; 154 | m_imageFadeTimer = new QTimer(this); 155 | m_imageFadeTimer->setInterval(PYP_IMAGE_FADE_TIMER_INTERVAL); 156 | connect(m_imageFadeTimer,SIGNAL(timeout()),this,SLOT(DoImageFading())); 157 | 158 | #endif 159 | } 160 | 161 | void myPhotoView::calcGeo() 162 | { 163 | /// RESIZE AND MOVE PREVBUTTON 164 | QPoint PrevMovePot(PYP_LEFT_SPACE,height() - PYP_TOP_SPACE - PYP_BUTTON_SIZE.height()); 165 | prevButton->move(PrevMovePot); 166 | prevButton->show(); 167 | 168 | 169 | /// RESIZE AND MOVE NEXTBUTTON 170 | QPoint NextMovePot(width() - PYP_LEFT_SPACE - PYP_BUTTON_SIZE.width(),height() - PYP_TOP_SPACE -PYP_BUTTON_SIZE.height()); 171 | nextButton->move(NextMovePot); 172 | nextButton->show(); 173 | 174 | /// RESIZE AND MOVE NUM 175 | qreal NumX = (qreal)width()/2 - (qreal)num->width()/2; 176 | qreal NumY = height() - PYP_TOP_SPACE - PYP_BUTTON_SIZE.height()/2 - num->height()/2; 177 | QPointF NumMovePot(NumX,NumY); 178 | num->move(NumMovePot.toPoint()); 179 | } 180 | 181 | void myPhotoView::paintEvent(QPaintEvent *) 182 | { 183 | QPainter painter(this); 184 | painter.setRenderHints(QPainter::Antialiasing|QPainter::HighQualityAntialiasing|QPainter::SmoothPixmapTransform); 185 | drawBg(&painter); 186 | drawImage(&painter); 187 | } 188 | 189 | void myPhotoView::showEvent(QShowEvent *) 190 | { 191 | calcGeo(); 192 | } 193 | 194 | void myPhotoView::resizeEvent(QResizeEvent *) 195 | { 196 | calcGeo(); 197 | } 198 | 199 | void myPhotoView::drawBg(QPainter *painter) 200 | { 201 | painter->save(); 202 | painter->setPen(Qt::NoPen); 203 | 204 | QLinearGradient BgGradient(QPoint(0,0),QPoint(0,height())); 205 | BgGradient.setColorAt(0.0,PYP_BG_START_COLOR); 206 | BgGradient.setColorAt(1.0,PYP_BG_END_COLOR); 207 | painter->setBrush(BgGradient); 208 | 209 | painter->drawRect(rect()); 210 | 211 | painter->restore(); 212 | } 213 | 214 | void myPhotoView::drawImage(QPainter *painter) 215 | { 216 | painter->save(); 217 | 218 | #ifdef PYP_IMAGE_MOTION_PATTERN_FADE 219 | 220 | painter->setOpacity(m_imageOpacity); 221 | qreal imageWidth = m_currImage->width(); 222 | qreal imageHeight = m_currImage->height(); 223 | qreal ImageX = rect().center().x() - imageWidth/2; 224 | qreal ImageY = rect().center().y() - imageHeight/2; 225 | QPointF ImagePot(ImageX,ImageY); 226 | painter->drawImage(ImagePot,*m_currImage); 227 | #endif 228 | 229 | 230 | 231 | painter->restore(); 232 | } 233 | 234 | 235 | 236 | #ifdef PYP_IMAGE_FADE_TIMER_INTERVAL 237 | void myPhotoView::DoImageFading() 238 | { 239 | m_imageOpacity += PYP_IMAGE_OPACITY_INCREMENT; 240 | 241 | if(m_imageOpacity > 1.0) 242 | { 243 | m_imageOpacity = 1.0; 244 | m_imageFadeTimer->stop(); 245 | } 246 | update(); 247 | } 248 | 249 | #endif 250 | 251 | 252 | void myPhotoView::load(const QString &strFolder) 253 | { 254 | m_strFolder = strFolder; 255 | 256 | QDir imageFolder(strFolder); 257 | 258 | if (imageFolder.exists()) 259 | { 260 | QStringList ImageList = imageFolder.entryList(PYP_IMAGE_SUFFIX); 261 | if (ImageList.count()>0) 262 | { 263 | QString strFullPath ; 264 | m_ImageVec.clear(); 265 | foreach(const QString strImage,ImageList) 266 | { 267 | strFullPath = tr("%1/%2").arg(strFolder).arg(strImage); 268 | QImage* img = new QImage(strFullPath); 269 | m_ImageVec.push_back(img); 270 | } 271 | 272 | if(m_ImageVec.size() > 0) 273 | { 274 | m_currImage = m_ImageVec.at(0); 275 | m_nCurrIndex = 0; 276 | } 277 | 278 | emit sig_setTotal(ImageList.count()); 279 | #ifdef PYP_IMAGE_MOTION_PATTERN_FADE 280 | m_imageFadeTimer->start(); 281 | #endif 282 | } 283 | } 284 | } 285 | 286 | void myPhotoView::showNext() 287 | { 288 | if(m_ImageVec.isEmpty()) 289 | { 290 | return ; 291 | } 292 | m_nCurrIndex++; 293 | 294 | if(m_nCurrIndex >= m_ImageVec.count()) 295 | { 296 | m_nCurrIndex = m_ImageVec.count() -1; 297 | } 298 | m_currImage = m_ImageVec.at(m_nCurrIndex); 299 | 300 | #ifdef PYP_IMAGE_MOTION_PATTERN_FADE 301 | m_imageOpacity = 0.0; 302 | m_imageFadeTimer->start(); 303 | #endif 304 | emit sig_setValue(m_nCurrIndex + 1); 305 | calcGeo(); 306 | } 307 | 308 | void myPhotoView::showPrevious() 309 | { 310 | if(m_ImageVec.isEmpty()) 311 | { 312 | return ; 313 | } 314 | 315 | m_nCurrIndex--; 316 | 317 | if(m_nCurrIndex < 0) 318 | { 319 | m_nCurrIndex = 0; 320 | } 321 | m_currImage = m_ImageVec.at(m_nCurrIndex); 322 | #ifdef PYP_IMAGE_MOTION_PATTERN_FADE 323 | m_imageOpacity = 0.0; 324 | m_imageFadeTimer->start(); 325 | #endif 326 | 327 | emit sig_setValue(m_nCurrIndex + 1); 328 | calcGeo(); 329 | } 330 | -------------------------------------------------------------------------------- /myphotoview.h: -------------------------------------------------------------------------------- 1 | #ifndef MYPHOTOVIEW_H 2 | #define MYPHOTOVIEW_H 3 | 4 | #ifndef PYNUM_H 5 | #define PYNUM_H 6 | 7 | #define PYN_HOLD_DURATION 1000 8 | #define PYN_TIMER_INTERVAL 20 9 | #define PYN_OPACITY_INCREMENT 0.05 10 | #define PYN_FIN_OPACITY 0.6 11 | #define PYN_WIDTH_FACTOR 2.5 12 | 13 | #define PYN_MAX_HEIGHT 30 14 | #define PYN_RECT_RADIUS 6 15 | 16 | #define PYN_BG_START_COLOR QColor(46,132,243) 17 | #define PYN_BG_END_COLOR QColor(39,110,203) 18 | 19 | #define PYN_TEXT_COLOR Qt::white 20 | 21 | #define PYP_TOP_SPACE 20 22 | #define PYP_LEFT_SPACE 15 23 | #define PYP_BUTTON_WIDTH 70 24 | #define PYP_BUTTON_SIZE QSize(PYP_BUTTON_WIDTH,PYP_BUTTON_WIDTH) 25 | 26 | #define PYP_BG_START_COLOR QColor(89,89,89) 27 | #define PYP_BG_END_COLOR QColor(56,56,56) 28 | #define PYP_IMAGE_SUFFIX QStringList() << "*.png" << "*.jpg" << "*.gif" << "*.jpeg" << "*.bmp" 29 | 30 | #define PYP_IMAGE_MOTION_PATTERN_FADE 31 | #ifdef PYP_IMAGE_MOTION_PATTERN_FADE 32 | #define PYP_IMAGE_FADE_TIMER_INTERVAL 40 33 | #define PYP_IMAGE_OPACITY_INCREMENT 0.05 34 | #endif 35 | 36 | 37 | 38 | #include 39 | #include 40 | 41 | class PYNum : public QWidget 42 | { 43 | Q_OBJECT 44 | public: 45 | PYNum(QWidget* parent = 0); 46 | 47 | public slots: 48 | void setTotal(int totalNum); 49 | 50 | void setValue(int value); 51 | 52 | 53 | protected: 54 | void paintEvent(QPaintEvent *); 55 | 56 | void showEvent(QShowEvent *); 57 | 58 | QSize sizeHint() const 59 | { 60 | return QSize(100,PYN_MAX_HEIGHT); 61 | } 62 | 63 | private: 64 | void drawBg(QPainter* painter); 65 | 66 | void drawText(QPainter* painter); 67 | 68 | 69 | private: 70 | int m_nTotal,m_nValue; 71 | 72 | QTimer* m_fadeTimer; 73 | QTimer* m_holdTimer; 74 | QTimer* m_hideTimer; 75 | 76 | qreal m_opacity; 77 | 78 | private slots: 79 | void DoFading(); 80 | void DoHiding(); 81 | }; 82 | 83 | #endif // PYNUM_H 84 | 85 | class myPhotoView : public QWidget 86 | { 87 | Q_OBJECT 88 | public: 89 | explicit myPhotoView(QWidget *parent = 0); 90 | 91 | public slots: 92 | void load(const QString& strFolder); 93 | 94 | void showNext(); 95 | 96 | void showPrevious(); 97 | 98 | protected: 99 | virtual void paintEvent(QPaintEvent *); 100 | 101 | 102 | QSize sizeHint() const 103 | { 104 | return QSize(400,400); 105 | } 106 | 107 | virtual void showEvent(QShowEvent *); 108 | 109 | virtual void resizeEvent(QResizeEvent *); 110 | private: 111 | void drawBg(QPainter* painter); 112 | 113 | void drawImage(QPainter* painter); 114 | 115 | 116 | private: 117 | void initVariables(); 118 | void calcGeo(); 119 | 120 | 121 | private: 122 | QString m_strFolder; 123 | 124 | int m_nTotal; 125 | int m_nCurrIndex; 126 | 127 | QToolButton* prevButton; 128 | QToolButton* nextButton; 129 | 130 | QVector m_ImageVec; 131 | QImage* m_currImage; 132 | 133 | PYNum* num; 134 | 135 | #ifdef PYP_IMAGE_MOTION_PATTERN_FADE 136 | qreal m_imageOpacity; 137 | QTimer* m_imageFadeTimer; 138 | #endif 139 | 140 | private slots: 141 | 142 | #ifdef PYP_IMAGE_FADE_TIMER_INTERVAL 143 | void DoImageFading(); 144 | #endif 145 | 146 | 147 | signals: 148 | void sig_setValue(int value); 149 | void sig_setTotal(int total); 150 | 151 | }; 152 | 153 | #endif // MYPHOTOVIEW_H 154 | -------------------------------------------------------------------------------- /myprogressbar.cpp: -------------------------------------------------------------------------------- 1 | #include "myprogressbar.h" 2 | 3 | myProgressBar::myProgressBar(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | this->initVariables(); 7 | } 8 | 9 | void myProgressBar::initVariables() 10 | { 11 | m_nCurrentIndex=0; 12 | m_nRectCount=(width()-2*TOP_SPACE)/RECT_WIDTH; 13 | updateTimer=new QTimer(this); 14 | updateTimer->setInterval(100); 15 | m_bReverse=false; 16 | connect(updateTimer,SIGNAL(timeout()),this,SLOT(UpdateGraph())); 17 | start(); 18 | } 19 | 20 | void myProgressBar::paintEvent(QPaintEvent *) 21 | { 22 | QPainter painter; 23 | painter.begin(this); 24 | painter.setRenderHint(QPainter::Antialiasing); 25 | drawBackground(&painter); 26 | drawRects(&painter); 27 | painter.end(); 28 | } 29 | 30 | void myProgressBar::drawBackground(QPainter *painter) 31 | { 32 | painter->save(); 33 | 34 | QLinearGradient bgGradient(QPointF(0,0),QPointF(0,height())); 35 | 36 | bgGradient.setColorAt(0.0,QColor(180,180,180)); 37 | bgGradient.setColorAt(0.2,QColor(30,30,30)); 38 | bgGradient.setColorAt(0.8,QColor(30,30,30)); 39 | bgGradient.setColorAt(1.0,QColor(180,180,180)); 40 | painter->setBrush(Qt::black); 41 | painter->setPen(Qt::black); 42 | painter->drawRoundRect(rect(),0,0); 43 | painter->restore(); 44 | } 45 | 46 | void myProgressBar::drawRects(QPainter *painter) 47 | { 48 | painter->save(); 49 | 50 | qreal initX=LEFT_SPACE; 51 | qreal top=TOP_SPACE; 52 | qreal bottom=height()-TOP_SPACE; 53 | m_nRectCount=(width()-2*TOP_SPACE)/RECT_WIDTH; 54 | 55 | for(int i=0;isetPen(Qt::NoPen); 62 | 63 | if(!m_bReverse) 64 | { 65 | if(i==m_nCurrentIndex) 66 | { 67 | rectGradient.setColorAt(0.0,QColor(30,100,30)); 68 | rectGradient.setColorAt(0.2,QColor(30,180,30)); 69 | rectGradient.setColorAt(0.8,QColor(30,180,30)); 70 | rectGradient.setColorAt(1.0,QColor(30,100,30)); 71 | 72 | 73 | } 74 | else if(i==(m_nCurrentIndex-1)) 75 | { 76 | rectGradient.setColorAt(0.0,QColor(30,100,30)); 77 | rectGradient.setColorAt(0.2,QColor(30,150,30)); 78 | rectGradient.setColorAt(0.8,QColor(30,150,30)); 79 | rectGradient.setColorAt(1.0,QColor(30,100,30)); 80 | 81 | } 82 | else if(i==(m_nCurrentIndex-2)) 83 | { 84 | rectGradient.setColorAt(0.0,QColor(30,100,30)); 85 | rectGradient.setColorAt(0.2,QColor(30,120,30)); 86 | rectGradient.setColorAt(0.8,QColor(30,120,30)); 87 | rectGradient.setColorAt(1.0,QColor(30,100,30)); 88 | 89 | } 90 | else if(i==(m_nCurrentIndex-3)) 91 | { 92 | rectGradient.setColorAt(0.0,QColor(30,100,30)); 93 | rectGradient.setColorAt(0.2,QColor(30,90,30)); 94 | rectGradient.setColorAt(0.8,QColor(30,90,30)); 95 | rectGradient.setColorAt(1.0,QColor(30,100,30)); 96 | 97 | } 98 | else 99 | { 100 | rectGradient.setColorAt(0.0,QColor(150,150,150)); 101 | rectGradient.setColorAt(0.2,QColor(30,30,30)); 102 | rectGradient.setColorAt(0.8,QColor(30,30,30)); 103 | rectGradient.setColorAt(1.0,QColor(150,150,150)); 104 | } 105 | } 106 | else 107 | { 108 | if(i==m_nCurrentIndex) 109 | { 110 | rectGradient.setColorAt(0.0,QColor(30,100,30)); 111 | rectGradient.setColorAt(0.2,QColor(30,180,30)); 112 | rectGradient.setColorAt(0.8,QColor(30,180,30)); 113 | rectGradient.setColorAt(1.0,QColor(30,100,30)); 114 | 115 | 116 | } 117 | else if(i==(m_nCurrentIndex+1)) 118 | { 119 | rectGradient.setColorAt(0.0,QColor(30,100,30)); 120 | rectGradient.setColorAt(0.2,QColor(30,150,30)); 121 | rectGradient.setColorAt(0.8,QColor(30,150,30)); 122 | rectGradient.setColorAt(1.0,QColor(30,100,30)); 123 | 124 | } 125 | else if(i==(m_nCurrentIndex+2)) 126 | { 127 | rectGradient.setColorAt(0.0,QColor(30,100,30)); 128 | rectGradient.setColorAt(0.2,QColor(30,120,30)); 129 | rectGradient.setColorAt(0.8,QColor(30,120,30)); 130 | rectGradient.setColorAt(1.0,QColor(30,100,30)); 131 | 132 | } 133 | else if(i==(m_nCurrentIndex+3)) 134 | { 135 | rectGradient.setColorAt(0.0,QColor(30,100,30)); 136 | rectGradient.setColorAt(0.2,QColor(30,90,30)); 137 | rectGradient.setColorAt(0.8,QColor(30,90,30)); 138 | rectGradient.setColorAt(1.0,QColor(30,100,30)); 139 | 140 | } 141 | else 142 | { 143 | rectGradient.setColorAt(0.0,QColor(150,150,150)); 144 | rectGradient.setColorAt(0.2,QColor(30,30,30)); 145 | rectGradient.setColorAt(0.8,QColor(30,30,30)); 146 | rectGradient.setColorAt(1.0,QColor(150,150,150)); 147 | } 148 | 149 | } 150 | painter->setBrush(rectGradient); 151 | painter->drawRoundedRect(singleRect,SMALL_ANGLE,SMALL_ANGLE); 152 | 153 | initX+=RECT_WIDTH+RECT_SPACE; 154 | } 155 | painter->restore(); 156 | } 157 | 158 | void myProgressBar::UpdateGraph() 159 | { 160 | if(!m_bReverse) 161 | { 162 | m_nCurrentIndex=(++m_nCurrentIndex)%m_nRectCount; 163 | 164 | if(m_nCurrentIndex==m_nRectCount-1) 165 | { 166 | m_bReverse=true; 167 | } 168 | } 169 | else 170 | { 171 | m_nCurrentIndex=(--m_nCurrentIndex)%m_nRectCount; 172 | if(m_nCurrentIndex==0) 173 | { 174 | m_bReverse=false; 175 | } 176 | } 177 | update(); 178 | } 179 | 180 | void myProgressBar::start() 181 | { 182 | updateTimer->start(); 183 | } 184 | 185 | void myProgressBar::stop() 186 | { 187 | updateTimer->stop(); 188 | } 189 | 190 | void myProgressBar::setInterval(int msec) 191 | { 192 | updateTimer->setInterval(msec); 193 | } 194 | -------------------------------------------------------------------------------- /myprogressbar.h: -------------------------------------------------------------------------------- 1 | #ifndef MYPROGRESSBAR_H 2 | #define MYPROGRESSBAR_H 3 | 4 | #define RECT_WIDTH 20 5 | #define LEFT_SPACE 3 6 | #define TOP_SPACE 2 7 | #define RECT_SPACE 3 8 | #define LARGE_ANGLE 20 9 | #define SMALL_ANGLE 30 10 | 11 | #include 12 | #include 13 | 14 | class myProgressBar : public QWidget 15 | { 16 | Q_OBJECT 17 | public: 18 | explicit myProgressBar(QWidget *parent = 0); 19 | 20 | protected: 21 | void paintEvent(QPaintEvent *); 22 | QSize sizeHint() const 23 | { 24 | return QSize(300,70); 25 | } 26 | 27 | private: 28 | void drawBackground(QPainter* painter); 29 | void drawRects(QPainter* painter); 30 | 31 | private: 32 | int m_nCurrentIndex; 33 | int m_nRectCount; 34 | QTimer* updateTimer; 35 | bool m_bReverse; 36 | 37 | private: 38 | void initVariables(); 39 | 40 | private slots: 41 | void UpdateGraph(); 42 | 43 | public slots: 44 | void start(); 45 | void stop(); 46 | void setInterval(int msec); 47 | 48 | }; 49 | 50 | #endif // MYPROGRESSBAR_H 51 | -------------------------------------------------------------------------------- /myprogressbar1.cpp: -------------------------------------------------------------------------------- 1 | #include "myprogressbar1.h" 2 | 3 | myProgressBar1::myProgressBar1(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | this->initVariables(); 7 | } 8 | 9 | void myProgressBar1::initVariables() 10 | { 11 | m_min = 0; 12 | m_max = 100; 13 | m_value = 0; 14 | } 15 | 16 | void myProgressBar1::paintEvent(QPaintEvent *) 17 | { 18 | QPainter painter(this); 19 | painter.setRenderHints(QPainter::Antialiasing|QPainter::HighQualityAntialiasing); 20 | drawBg(&painter); 21 | drawBar(&painter); 22 | } 23 | 24 | void myProgressBar1::drawBg(QPainter *painter) 25 | { 26 | painter->save(); 27 | painter->setPen(PYP_BG_COLOR); 28 | 29 | QLinearGradient BgGradient(QPoint(0,0),QPoint(width(),0)); 30 | BgGradient.setColorAt(0.0,QColor(222,222,222)); 31 | BgGradient.setColorAt(0.05,PYP_BG_COLOR); 32 | BgGradient.setColorAt(1.0,PYP_BG_COLOR); 33 | painter->setBrush(BgGradient); 34 | painter->drawRoundedRect(rect(),PYP_RECT_RADIUS,PYP_RECT_RADIUS); 35 | 36 | painter->restore(); 37 | } 38 | 39 | void myProgressBar1::drawBar(QPainter *painter) 40 | { 41 | painter->save(); 42 | 43 | // DRAW UNDER BAR 44 | painter->setPen(Qt::NoPen); 45 | 46 | QPointF uTopLeft(PYP_LEFT_SPACE1,PYP_TOP_SPACE1); 47 | QPointF uBottomRight(width() - PYP_RIGHT_SPACE1 * 2 - PYP_TEXT_WIDTH,height() - PYP_TOP_SPACE1); 48 | QRectF uRect(uTopLeft,uBottomRight); 49 | painter->setBrush(PYP_UNDER_BAR_COLOR); 50 | painter->drawRoundedRect(uRect,PYP_RECT_RADIUS,PYP_RECT_RADIUS); 51 | 52 | 53 | // DRAW REAL BAR 54 | qreal range = (m_max - m_min); 55 | qreal RectWidth = width() - PYP_LEFT_SPACE1 - PYP_RIGHT_SPACE1 * 2 - PYP_TEXT_WIDTH; 56 | qreal dX = (qreal) RectWidth/range; 57 | 58 | 59 | qreal initX = PYP_LEFT_SPACE1; 60 | QPointF TopLeft(initX,PYP_TOP_SPACE1); 61 | QPointF BottomRight(initX + dX * m_value,height() - PYP_TOP_SPACE1); 62 | QRectF BarRect(TopLeft,BottomRight); 63 | 64 | QLinearGradient BarGradient(uRect.topLeft(),uRect.topRight()); 65 | BarGradient.setColorAt(0.0,PYP_BAR_START_COLOR); 66 | BarGradient.setColorAt(0.5,PYP_BAR_MIDDLE_COLOR); 67 | BarGradient.setColorAt(1.0,PYP_BAR_END_COLOR); 68 | 69 | painter->setBrush(BarGradient); 70 | 71 | painter->drawRoundedRect(BarRect,PYP_RECT_RADIUS,PYP_RECT_RADIUS); 72 | 73 | // DRAW COVER LINES 74 | 75 | int nLineCnt = uRect.width()/PYP_D_LINE; 76 | int dLineX = (qreal)uRect.width()/(nLineCnt - 1); 77 | painter->setPen(PYP_LINE_COLOR); 78 | while(initX < uRect.width()) 79 | { 80 | QPointF topPot(initX + dLineX,PYP_TOP_SPACE1); 81 | QPointF bottomPot(initX + dLineX,height() - PYP_TOP_SPACE1); 82 | painter->drawLine(topPot,bottomPot); 83 | 84 | /// increment initX 85 | initX += dLineX; 86 | 87 | } 88 | 89 | // draw text 90 | QPointF TextTopLeft(width() - PYP_RIGHT_SPACE1 - PYP_TEXT_WIDTH,PYP_TOP_SPACE1); 91 | QPointF TextBottomRight(width() - PYP_RIGHT_SPACE1,height() - PYP_TOP_SPACE1); 92 | QRectF TextRect(TextTopLeft,TextBottomRight); 93 | painter->setPen(PYP_UNDER_BAR_COLOR); 94 | painter->setBrush(PYP_UNDER_BAR_COLOR); 95 | painter->drawRoundedRect(TextRect,PYP_RECT_RADIUS,PYP_RECT_RADIUS); 96 | 97 | painter->setPen(PYP_TEXT_COLOR); 98 | 99 | QFont TextFont("Times New Roman"); 100 | TextFont.setBold(true); 101 | painter->setFont(TextFont); 102 | 103 | QString strText; 104 | qreal value = (qreal)m_value * 100/(m_max - m_min); 105 | strText = tr("%1%").arg(value,3,'f',1,'0'); 106 | painter->drawText(TextRect,strText,Qt::AlignVCenter|Qt::AlignHCenter); 107 | painter->restore(); 108 | } 109 | 110 | void myProgressBar1::setRange(qreal min, qreal max) 111 | { 112 | Q_ASSERT(min < max); 113 | m_min = min; 114 | m_max = max; 115 | update(); 116 | } 117 | 118 | void myProgressBar1::setValue(qreal value) 119 | { 120 | m_value = value; 121 | update(); 122 | } 123 | 124 | -------------------------------------------------------------------------------- /myprogressbar1.h: -------------------------------------------------------------------------------- 1 | #ifndef MYPROGRESSBAR1_H 2 | #define MYPROGRESSBAR1_H 3 | 4 | #define PYP_LEFT_SPACE1 10 5 | #define PYP_TOP_SPACE1 5 6 | #define PYP_RIGHT_SPACE1 7 7 | #define PYP_TEXT_WIDTH 45 8 | #define PYP_RECT_RADIUS 8 9 | #define PYP_LINE_COUNT 20 10 | #define PYP_LINE_WIDTH 2 11 | #define PYP_D_LINE 7 12 | 13 | #define PYP_LINE_COLOR Qt::black 14 | #define PYP_BG_COLOR QColor(45,45,45) 15 | 16 | #define PYP_BAR_START_COLOR QColor(85,255,0) 17 | #define PYP_BAR_MIDDLE_COLOR QColor(249,216,47) 18 | #define PYP_BAR_END_COLOR QColor(249,107,24) 19 | #define PYP_UNDER_BAR_COLOR QColor(121,121,121) 20 | 21 | #define PYP_TEXT_COLOR Qt::white 22 | 23 | #include 24 | #include 25 | 26 | class myProgressBar1 : public QWidget 27 | { 28 | Q_OBJECT 29 | public: 30 | explicit myProgressBar1(QWidget *parent = 0); 31 | 32 | public: 33 | void setRange(qreal min,qreal max); 34 | 35 | void setValue(qreal value); 36 | 37 | protected: 38 | void paintEvent(QPaintEvent *); 39 | 40 | QSize sizeHint() const 41 | { 42 | return QSize(400,50); 43 | } 44 | 45 | QSize minimumSizeHint() const 46 | { 47 | return QSize(200,40); 48 | } 49 | 50 | 51 | private: 52 | /// painting functions 53 | void drawBg(QPainter* painter); 54 | 55 | void drawBar(QPainter* painter); 56 | 57 | 58 | 59 | private: 60 | void initVariables(); 61 | 62 | private: 63 | qreal m_min; 64 | qreal m_max; 65 | qreal m_value; 66 | 67 | }; 68 | 69 | #endif // MYPROGRESSBAR1_H 70 | -------------------------------------------------------------------------------- /mysliderbutton.cpp: -------------------------------------------------------------------------------- 1 | #include "mysliderbutton.h" 2 | 3 | mySliderButton::mySliderButton(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | myBGColor1=QColor::fromRgb(46,132,243); 7 | myBGColor2=QColor::fromRgb(39,110,203); 8 | 9 | m_bForward = true; 10 | m_bShrink = false; 11 | m_bVirgin = true; 12 | 13 | m_slideTimer = new QTimer(this); 14 | m_slideTimer->setInterval(PYSB_SLIDE_TIMER_INTERVAL); 15 | connect(m_slideTimer,SIGNAL(timeout()),this,SLOT(DoSliding())); 16 | 17 | m_shrinkTimer = new QTimer(this); 18 | m_shrinkTimer->setInterval(PYSB_SHRINK_TIMER_INTERVAL); 19 | connect(m_shrinkTimer,SIGNAL(timeout()),this,SLOT(DoShrinking())); 20 | 21 | setFixedHeight(PYSB_HEIGHT); 22 | setAttribute(Qt::WA_TranslucentBackground); 23 | } 24 | 25 | mySliderButton::~mySliderButton() 26 | { 27 | 28 | } 29 | 30 | void mySliderButton::calcGeo() 31 | { 32 | 33 | } 34 | 35 | void mySliderButton::mousePressEvent(QMouseEvent *e) 36 | { 37 | int ItemCnt = m_StrRectPairVec.count(); 38 | QString strText; 39 | for(int ItemIndex = 0;ItemIndex < ItemCnt ; ItemIndex++) 40 | { 41 | if(m_StrRectPairVec.at(ItemIndex).second.contains(e->pos())) 42 | { 43 | strText = m_StrRectPairVec.at(ItemIndex).first; 44 | DEBUG_VAR(strText); 45 | 46 | emit sig_itemClicked(strText); 47 | 48 | m_TargetRect = m_StrRectPairVec.at(ItemIndex).second; 49 | 50 | if(m_TargetRect.width() > m_BarRect.width()) 51 | { 52 | m_bShrink = false; 53 | }else{ 54 | m_bShrink = true; 55 | } 56 | 57 | if(m_TargetRect.topLeft().x() > m_BarRect.topLeft().x() ) 58 | { 59 | m_bForward = true; 60 | }else{ 61 | m_bForward = false; 62 | } 63 | 64 | int distance = m_TargetRect.topLeft().x() - m_BarRect.topLeft().x(); 65 | if(distance < 0) 66 | distance = 0 - distance; 67 | QString tmp; 68 | tmp = QString("%1").arg(distance); 69 | DEBUG_VAR(tmp); 70 | 71 | 72 | SetSlideIncrement(GetSlideIncrementInit((unsigned int)distance)/*PYSB_BAR_SLIDE_INCREMENT_INIT*/); 73 | m_slideTimer->start(); 74 | FIREUP_SHRINK_TIMER; 75 | } 76 | } 77 | } 78 | 79 | void mySliderButton::paintEvent(QPaintEvent *) 80 | { 81 | QPainter painter(this); 82 | painter.setRenderHints(QPainter::Antialiasing|QPainter::HighQualityAntialiasing); 83 | drawBg(&painter); 84 | drawBar(&painter); 85 | drawText(&painter); 86 | } 87 | 88 | void mySliderButton::SetmyBGColor(QColor c1, QColor c2) 89 | { 90 | myBGColor1=c1; 91 | myBGColor2=c2; 92 | } 93 | 94 | void mySliderButton::drawBar(QPainter *painter) 95 | { 96 | painter->save(); 97 | painter->setPen(Qt::NoPen); 98 | 99 | QLinearGradient BarGradient(m_BarRect.topLeft(),m_BarRect.bottomLeft()); 100 | // BarGradient.setColorAt(0.0,PYSB_BAR_START_COLOR); 101 | // BarGradient.setColorAt(1.0,PYSB_BAR_END_COLOR); 102 | BarGradient.setColorAt(0.0,myBGColor1); 103 | BarGradient.setColorAt(1.0,myBGColor2); 104 | painter->setBrush(BarGradient); 105 | 106 | painter->drawRoundedRect(m_BarRect,PYSB_RECT_RADIUS,PYSB_RECT_RADIUS); 107 | painter->restore(); 108 | } 109 | 110 | void mySliderButton::drawBg(QPainter *painter) 111 | { 112 | painter->save(); 113 | painter->setPen(Qt::NoPen); 114 | 115 | QLinearGradient BgGradient(QPoint(0,0),QPoint(0,height())); 116 | BgGradient.setColorAt(0.0,PYSB_BG_START_COLOR); 117 | BgGradient.setColorAt(1.0,PYSB_BG_END_COLOR); 118 | painter->setBrush(BgGradient); 119 | painter->drawRoundedRect(rect(),PYSB_RECT_RADIUS,PYSB_RECT_RADIUS); 120 | 121 | 122 | painter->restore(); 123 | } 124 | 125 | void mySliderButton::drawText(QPainter *painter) 126 | { 127 | painter->save(); 128 | 129 | painter->setPen(PYSB_TEXT_COLOR); 130 | QFont textFont; 131 | textFont.setBold(true); 132 | painter->setFont(textFont); 133 | 134 | int ItemCnt = m_StrRectPairVec.count(); 135 | qreal initX = 0; 136 | qreal textWidth = 0; 137 | QString strText; 138 | for(int ItemIndex = 0;ItemIndex < ItemCnt ;ItemIndex++) 139 | { 140 | strText = m_StrRectPairVec.at(ItemIndex).first; 141 | textWidth = fontMetrics().width(strText); 142 | QPointF topLeft(initX,0); 143 | QPointF bottomRight(initX + textWidth + 2 * PYSB_EXTRA_SPACE,height()); 144 | QRectF textRect(topLeft,bottomRight); 145 | 146 | m_StrRectPairVec[ItemIndex].second = textRect; 147 | if(m_bVirgin) 148 | { 149 | m_BarRect = textRect; 150 | m_bVirgin = false; 151 | } 152 | painter->drawText(textRect,strText,Qt::AlignVCenter|Qt::AlignHCenter); 153 | 154 | /// increment initX 155 | initX +=textWidth + 2 * PYSB_EXTRA_SPACE; 156 | } 157 | 158 | setFixedWidth(initX); 159 | painter->restore(); 160 | } 161 | 162 | unsigned int mySliderButton::GetSlideIncrement() 163 | { 164 | if(m_nInc > 1) 165 | m_nInc --; 166 | 167 | return m_nInc; 168 | } 169 | 170 | void mySliderButton::SetSlideIncrement(unsigned int nInc) 171 | { 172 | m_nInc = nInc; 173 | } 174 | 175 | unsigned int mySliderButton::GetSlideIncrementInit(unsigned int nDistance) 176 | { 177 | unsigned int n = 1; 178 | 179 | while(1) 180 | { 181 | if(n*n>nDistance) 182 | break; 183 | else 184 | n++; 185 | } 186 | 187 | return n*1.4; 188 | } 189 | 190 | void mySliderButton::DoSliding() 191 | { 192 | 193 | qreal BarX = m_BarRect.topLeft().x(); 194 | qreal BarWidth = m_BarRect.width(); 195 | if(m_bForward) 196 | { 197 | BarX += GetSlideIncrement()/*PYSB_BAR_SLIDE_INCREMENT*/; 198 | if(BarX >= m_TargetRect.topLeft().x()) 199 | { 200 | BarX = m_TargetRect.topLeft().x(); 201 | m_slideTimer->stop(); 202 | //FIREUP_SHRINK_TIMER; 203 | //SetSlideIncrement(PYSB_BAR_SLIDE_INCREMENT_INIT); 204 | } 205 | }else{ 206 | BarX -= GetSlideIncrement()/*PYSB_BAR_SLIDE_INCREMENT*/; 207 | if(BarX <= m_TargetRect.topLeft().x()) 208 | { 209 | BarX = m_TargetRect.topLeft().x(); 210 | m_slideTimer->stop(); 211 | //FIREUP_SHRINK_TIMER; 212 | //SetSlideIncrement(PYSB_BAR_SLIDE_INCREMENT_INIT); 213 | } 214 | } 215 | 216 | m_BarRect = QRectF(QPointF(BarX,0),QPointF(BarX + BarWidth,height())); 217 | update(); 218 | } 219 | 220 | void mySliderButton::DoShrinking() 221 | { 222 | qreal BarX = m_BarRect.topLeft().x(); 223 | qreal BarWidth = m_BarRect.width(); 224 | 225 | if(m_bShrink) 226 | { 227 | BarWidth -= PYSB_BAR_WIDTH_INCREMENT; 228 | 229 | if(BarWidth < m_TargetRect.width()) 230 | { 231 | m_shrinkTimer->stop(); 232 | } 233 | 234 | }else{ 235 | BarWidth += PYSB_BAR_WIDTH_INCREMENT; 236 | 237 | if(BarWidth > m_TargetRect.width()) 238 | { 239 | m_shrinkTimer->stop(); 240 | } 241 | 242 | } 243 | m_BarRect = QRectF(QPointF(BarX,0),QPointF(BarX + BarWidth,height())); 244 | update(); 245 | } 246 | 247 | void mySliderButton::addItem(const QString &str) 248 | { 249 | int ItemCnt = m_StrRectPairVec.count(); 250 | for(int ItemIndex = 0;ItemIndex < ItemCnt ; ItemIndex++) 251 | { 252 | if(m_StrRectPairVec.at(ItemIndex).first.toLower() == str.toLower()) 253 | { 254 | return ; 255 | } 256 | } 257 | 258 | m_StrRectPairVec.push_back(qMakePair(str,QRectF())); 259 | update(); 260 | 261 | if(m_StrRectPairVec.count() > 0) 262 | { 263 | emit sig_itemClicked(m_StrRectPairVec.at(0).first); 264 | } 265 | 266 | calcGeo(); 267 | } 268 | -------------------------------------------------------------------------------- /mysliderbutton.h: -------------------------------------------------------------------------------- 1 | #ifndef MYSLIDEBUTTON_H 2 | #define MYSLIDEBUTTON_H 3 | 4 | #define PYSB_EXTRA_SPACE 15 5 | #define PYSB_RECT_RADIUS 8 6 | #define PYSB_HEIGHT 30 7 | 8 | #define PYSB_BG_START_COLOR QColor(121,121,121) 9 | #define PYSB_BG_END_COLOR QColor(78,78,78) 10 | 11 | #define PYSB_BAR_SKIN1 12 | 13 | #ifdef PYSB_BAR_SKIN1 14 | #define PYSB_BAR_START_COLOR QColor(46,132,243) 15 | #define PYSB_BAR_END_COLOR QColor(39,110,203) 16 | #endif 17 | 18 | #ifdef PYSB_BAR_SKIN2 19 | #define PYSB_BAR_START_COLOR QColor(255,177,42) 20 | #define PYSB_BAR_END_COLOR QColor(225,156,37) 21 | #endif 22 | 23 | #ifdef PYSB_BAR_SKIN3 24 | #define PYSB_BAR_START_COLOR QColor(148,255,82) 25 | #define PYSB_BAR_END_COLOR QColor(133,229,73) 26 | #endif 27 | 28 | #define PYSB_TEXT_COLOR Qt::white 29 | 30 | #define PYSB_BAR_WIDTH_INCREMENT 2 31 | #define PYSB_BAR_SLIDE_INCREMENT 3 32 | 33 | #define PYSB_SLIDE_TIMER_INTERVAL 20 34 | #define PYSB_SHRINK_TIMER_INTERVAL 20//30 35 | 36 | #define FIREUP_SHRINK_TIMER if(m_BarRect.width() > m_TargetRect.width()) \ 37 | { \ 38 | m_bShrink = true; \ 39 | m_shrinkTimer->start(); \ 40 | }else{ \ 41 | m_bShrink = false; \ 42 | m_shrinkTimer->start(); \ 43 | } 44 | 45 | 46 | 47 | #ifdef DEBUG_VAR 48 | #undef DEBUG_VAR 49 | #endif 50 | #define DEBUG_VAR(VAR) qDebug() << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << #VAR << ":" << VAR; 51 | 52 | #include 53 | #include 54 | #include 55 | #include 56 | #include 57 | #include 58 | #include 59 | 60 | class mySliderButton : public QWidget 61 | { 62 | Q_OBJECT 63 | public: 64 | explicit mySliderButton(QWidget *parent = 0); 65 | ~mySliderButton(); 66 | 67 | public: 68 | void addItem(const QString& str); 69 | void SetmyBGColor(QColor c1,QColor c2); 70 | 71 | protected: 72 | virtual void mousePressEvent(QMouseEvent *); 73 | virtual void paintEvent(QPaintEvent *); 74 | QSize sizeHint() const 75 | { 76 | return QSize(300,50); 77 | } 78 | 79 | private: 80 | void drawBg(QPainter* painter); 81 | void drawBar(QPainter* painter); 82 | void drawText(QPainter* painter); 83 | 84 | private: 85 | void calcGeo(); 86 | 87 | private: 88 | QVector< QPair > m_StrRectPairVec; 89 | QRectF m_BarRect; 90 | QRectF m_TargetRect; 91 | 92 | qreal m_totalLength; 93 | 94 | bool m_bForward; 95 | bool m_bShrink; 96 | bool m_bVirgin; 97 | 98 | QTimer* m_slideTimer; 99 | QTimer* m_shrinkTimer; 100 | 101 | unsigned int m_nInc; 102 | unsigned int GetSlideIncrement(); 103 | void SetSlideIncrement(unsigned int nInc); 104 | 105 | unsigned int GetSlideIncrementInit(unsigned int nDistance); 106 | 107 | QColor myBGColor1; 108 | QColor myBGColor2; 109 | 110 | private slots: 111 | void DoSliding(); 112 | void DoShrinking(); 113 | 114 | signals: 115 | void sig_itemClicked(const QString& str); 116 | 117 | }; 118 | 119 | #endif // MYSLIDEBUTTON_H 120 | -------------------------------------------------------------------------------- /myvolume.cpp: -------------------------------------------------------------------------------- 1 | #include "myvolume.h" 2 | 3 | myVolume::myVolume(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | this->initVariables(); 7 | } 8 | 9 | void myVolume::initVariables() 10 | { 11 | m_nValue = 0; 12 | } 13 | 14 | void myVolume::paintEvent(QPaintEvent *) 15 | { 16 | QPainter painter(this); 17 | painter.setRenderHints(QPainter::Antialiasing); 18 | drawBg(&painter); 19 | draw(&painter); 20 | } 21 | 22 | 23 | void myVolume::drawBg(QPainter *painter) 24 | { 25 | painter->save(); 26 | painter->setPen(Qt::NoPen); 27 | painter->setBrush(PYV_BG_COLOR); 28 | painter->drawRect(rect()); 29 | painter->restore(); 30 | } 31 | 32 | void myVolume::draw(QPainter *painter) 33 | { 34 | painter->save(); 35 | 36 | qreal dY = (qreal)(height() - (PYV_BAR_COUNT + 1 ) * PYV_TOP_SPACE)/PYV_BAR_COUNT; 37 | 38 | qreal initY = PYV_TOP_SPACE; 39 | for(int BarIndex = 0;BarIndex < PYV_BAR_COUNT;BarIndex++) 40 | { 41 | QPointF BarTopLeft(PYV_LEFT_SPACE,initY); 42 | QPointF BarBottomRight(width() - PYV_LEFT_SPACE,initY + dY); 43 | QRectF BarRect(BarTopLeft,BarBottomRight); 44 | 45 | QLinearGradient BarGradient(BarRect.topLeft(),BarRect.bottomLeft()); 46 | BarGradient.setColorAt(0.0,PYV_BAR_DEFAULT_END_COLOR); 47 | BarGradient.setColorAt(0.5,PYV_BAR_DEFAULT_START_COLOR); 48 | BarGradient.setColorAt(1.0,PYV_BAR_DEFAULT_END_COLOR); 49 | 50 | painter->setPen(Qt::NoPen); 51 | painter->setOpacity(PYV_OPACITY); 52 | painter->setBrush(BarGradient); 53 | 54 | painter->drawRoundedRect(BarRect,PYV_RECT_RADIUS,PYV_RECT_RADIUS); 55 | 56 | /// increment initY 57 | initY += dY + PYV_TOP_SPACE; 58 | } 59 | 60 | // draw value bar 61 | painter->save(); 62 | painter->setPen(Qt::NoPen); 63 | painter->setOpacity(1.0); 64 | 65 | QLinearGradient ValueBarGradient(QPointF(0,height() - PYV_TOP_SPACE),QPointF(0,PYV_TOP_SPACE)); 66 | ValueBarGradient.setColorAt(0.0,PYV_BAR_START_COLOR); 67 | ValueBarGradient.setColorAt(0.5,PYV_BAR_MIDDLE_COLOR); 68 | ValueBarGradient.setColorAt(1.0,PYV_BAR_END_COLOR); 69 | painter->setBrush(ValueBarGradient); 70 | 71 | qreal rInitY = PYV_TOP_SPACE*((PYV_BAR_COUNT - m_nValue) + 1)+(PYV_BAR_COUNT - m_nValue )* dY ; 72 | for(int ValueIndex = 0;ValueIndex < m_nValue; ValueIndex++) 73 | { 74 | QPointF BarTopLeft(PYV_LEFT_SPACE,rInitY); 75 | QPointF BarBottomRight(width() - PYV_LEFT_SPACE,rInitY + dY); 76 | QRectF BarRect(BarTopLeft,BarBottomRight); 77 | painter->drawRoundedRect(BarRect,PYV_RECT_RADIUS,PYV_RECT_RADIUS); 78 | 79 | /// decrement initY 80 | rInitY += dY + PYV_TOP_SPACE; 81 | } 82 | 83 | painter->restore(); 84 | painter->restore(); 85 | } 86 | 87 | void myVolume::DoDecrement() 88 | { 89 | 90 | } 91 | 92 | 93 | void myVolume::setValue(int value) 94 | { 95 | m_nValue = value; 96 | update(); 97 | } 98 | 99 | int myVolume::value() const 100 | { 101 | return m_nValue; 102 | } 103 | 104 | -------------------------------------------------------------------------------- /myvolume.h: -------------------------------------------------------------------------------- 1 | #ifndef MYVOLUME_H 2 | #define MYVOLUME_H 3 | 4 | #define PYV_LEFT_SPACE 5 5 | #define PYV_TOP_SPACE 3 6 | #define PYV_TIMER_INTERVAL 500 7 | #define PYV_BAR_COUNT 10 8 | #define PYV_RECT_RADIUS 3 9 | #define PYV_OPACITY 0.4 10 | 11 | #define PYV_BAR_DEFAULT_START_COLOR QColor(167,167,167) 12 | #define PYV_BAR_DEFAULT_END_COLOR QColor(45,45,45) 13 | #define PYV_BG_COLOR Qt::black 14 | 15 | #define PYV_BAR_START_COLOR QColor(85,255,0) 16 | #define PYV_BAR_MIDDLE_COLOR QColor(249,216,47) 17 | #define PYV_BAR_END_COLOR QColor(249,107,24) 18 | 19 | #include 20 | #include 21 | 22 | 23 | class myVolume : public QWidget 24 | { 25 | Q_OBJECT 26 | public: 27 | explicit myVolume(QWidget *parent = 0); 28 | 29 | public: 30 | void setValue(int value); 31 | int value() const; 32 | 33 | protected: 34 | void paintEvent(QPaintEvent *); 35 | 36 | QSize sizeHint() const 37 | { 38 | return QSize(80,200); 39 | } 40 | 41 | 42 | private: 43 | void drawBg(QPainter* painter); 44 | void draw(QPainter* painter); 45 | 46 | private: 47 | void initVariables(); 48 | int m_nValue; 49 | QTimer* m_decreTimer; 50 | 51 | private slots: 52 | void DoDecrement(); 53 | 54 | }; 55 | 56 | #endif // MYVOLUME_H 57 | -------------------------------------------------------------------------------- /mywave.cpp: -------------------------------------------------------------------------------- 1 | #include "mywave.h" 2 | 3 | myWave::myWave(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | this->initVariables(); 7 | } 8 | 9 | void myWave::initVariables() 10 | { 11 | m_nChannelCount = 0; 12 | 13 | m_xIncrement = 0; 14 | 15 | m_updateTimer=new QTimer(this); 16 | m_updateTimer->setInterval(PYW_UPDATE_INTERVAL); 17 | connect(m_updateTimer,SIGNAL(timeout()),this,SLOT(UpdateData())); 18 | } 19 | 20 | void myWave::resizeEvent(QResizeEvent *) 21 | { 22 | m_xIncrement = (qreal)width()/m_nChannelCount; 23 | } 24 | 25 | void myWave::paintEvent(QPaintEvent *) 26 | { 27 | QPainter painter(this); 28 | drawBg(&painter); 29 | drawWave(&painter); 30 | } 31 | 32 | 33 | void myWave::drawBg(QPainter *painter) 34 | { 35 | painter->save(); 36 | painter->setPen(Qt::NoPen); 37 | 38 | QLinearGradient BgGradient(QPoint(0,0),QPoint(0,height())); 39 | BgGradient.setColorAt(0.0,PYW_BG_END_COLOR); 40 | // BgGradient.setColorAt(0.5,PYW_BG_END_COLOR); 41 | BgGradient.setColorAt(1.0,PYW_BG_END_COLOR); 42 | painter->setBrush(BgGradient); 43 | painter->drawRect(rect()); 44 | 45 | painter->restore(); 46 | } 47 | 48 | void myWave::drawWave(QPainter *painter) 49 | { 50 | painter->save(); 51 | 52 | if(ANTIALIASING_ON) 53 | { 54 | painter->setRenderHints(QPainter::Antialiasing|QPainter::HighQualityAntialiasing); 55 | } 56 | 57 | QVector PotVec; 58 | 59 | qreal startX = 0; // DEBUG_VAR(ChannelIndex); 60 | for(int DataIndex = 0 ; DataIndex < m_CurrDataVec.count() - 1; ++DataIndex) 61 | { 62 | // DEBUG_VAR(DataIndex); 63 | qreal CurrValue=m_CurrDataVec.at(DataIndex); 64 | qreal dY1=(qreal)height()/PYW_RANGE*CurrValue; 65 | qreal y1=height()-dY1; 66 | qreal NextValue=m_CurrDataVec.at(DataIndex+1); 67 | qreal dY2=(qreal)height()/PYW_RANGE*NextValue; 68 | qreal y2=height()-dY2; 69 | QPointF dataPot1(startX,y1); 70 | QPointF dataPot2(startX+m_xIncrement,y2); 71 | startX += m_xIncrement; 72 | PotVec.push_back(dataPot1); 73 | PotVec.push_back(dataPot2); 74 | } 75 | 76 | painter->setPen(QPen(PYW_CURVE_COLOR,PYW_PEN_WIDTH)); 77 | painter->drawLines(PotVec); 78 | painter->restore(); 79 | } 80 | 81 | void myWave::UpdateData() 82 | { 83 | for(int DataIndex = 0 ;DataIndex < m_nChannelCount ; DataIndex++) 84 | { 85 | qApp->processEvents(); 86 | 87 | if(m_CurrDataVec.at(DataIndex) < m_DataVec.at(DataIndex)) 88 | { 89 | m_CurrDataVec[DataIndex] += PYW_VALUE_INCREMENT; 90 | }else if(m_CurrDataVec.at(DataIndex) > m_DataVec.at(DataIndex)){ 91 | m_CurrDataVec[DataIndex] -= PYW_VALUE_INCREMENT; 92 | }else{ 93 | continue ; 94 | } 95 | } 96 | 97 | update(); 98 | } 99 | 100 | void myWave::setChannelCount(int cnt) 101 | { 102 | m_nChannelCount = cnt; 103 | m_CurrDataVec.resize(cnt); 104 | m_DataVec.resize(cnt); 105 | 106 | m_xIncrement = (qreal)width()/m_nChannelCount; 107 | } 108 | 109 | void myWave::setChannelData(const QVector DataVec) 110 | { 111 | int DataCnt = DataVec.count(); 112 | for(int DataIndex = 0;DataIndex < DataCnt ; DataIndex++) 113 | { 114 | m_DataVec[DataIndex] = DataVec.at(DataIndex); 115 | } 116 | 117 | if(!m_updateTimer->isActive()) 118 | { 119 | m_updateTimer->start(); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /mywave.h: -------------------------------------------------------------------------------- 1 | #ifndef MYWAVE_H 2 | #define MYWAVE_H 3 | 4 | #define PYW_BG_START_COLOR QColor(187,187,187) 5 | #define PYW_BG_END_COLOR QColor(67,67,67) 6 | 7 | #define PYW_CURVE_COLOR QColor(69,131,189) 8 | #define PYW_PEN_WIDTH 2 9 | #define PYW_LEFT_SPACE 10 10 | #define PYW_TOP_SPACE 10 11 | #define PYW_UPDATE_INTERVAL 20 12 | #define PYW_VALUE_INCREMENT 1 13 | #define PYW_RANGE 100 14 | 15 | #define ANTIALIASING_ON true 16 | 17 | #include 18 | #include 19 | 20 | class myWave : public QWidget 21 | { 22 | Q_OBJECT 23 | public: 24 | explicit myWave(QWidget *parent = 0); 25 | 26 | public: 27 | /// public interfaces 28 | void setChannelCount(int cnt); 29 | 30 | void setChannelData(const QVector DataVec); 31 | 32 | protected: 33 | void paintEvent(QPaintEvent *); 34 | 35 | QSize sizeHint() const 36 | { 37 | return QSize(500,120); 38 | } 39 | 40 | void resizeEvent(QResizeEvent *); 41 | 42 | private: 43 | void drawBg(QPainter* painter); 44 | 45 | void drawWave(QPainter* painter); 46 | 47 | 48 | private: 49 | QTimer* m_updateTimer; 50 | 51 | private slots: 52 | void UpdateData(); 53 | 54 | private: 55 | void initVariables(); 56 | 57 | private: 58 | int m_nChannelCount; 59 | 60 | QVector m_CurrDataVec; 61 | QVector m_DataVec; 62 | 63 | qreal m_xIncrement; 64 | 65 | }; 66 | 67 | #endif // MYWAVE_H 68 | -------------------------------------------------------------------------------- /mywave1.cpp: -------------------------------------------------------------------------------- 1 | #include "mywave1.h" 2 | 3 | myWave1::myWave1(QWidget *parent) : 4 | QWidget(parent) 5 | { 6 | this->initVariables(); 7 | } 8 | 9 | void myWave1::initVariables() 10 | { 11 | m_nMin = 0; 12 | m_nMax = 100; 13 | m_nValue = 0; 14 | 15 | m_headY = 0; 16 | m_barY = 0; 17 | 18 | m_dropTimer=new QTimer(this); 19 | m_dropTimer->setInterval(15); 20 | connect(m_dropTimer,SIGNAL(timeout()),this,SLOT(DoDropHead())); 21 | 22 | m_toolTimer=new QTimer(this); 23 | m_toolTimer->setInterval(500); 24 | m_toolTimer->setSingleShot(true); 25 | connect(m_toolTimer,SIGNAL(timeout()),m_dropTimer,SLOT(start())); 26 | 27 | m_shrinkTimer=new QTimer(this); 28 | m_shrinkTimer->setInterval(5); 29 | connect(m_shrinkTimer,SIGNAL(timeout()),this,SLOT(DoShrinkBar())); 30 | 31 | setMaximumWidth(PYI_MAX_WIDTH); 32 | 33 | } 34 | 35 | void myWave1::paintEvent(QPaintEvent *) 36 | { 37 | QPainter painter(this); 38 | drawBackground(&painter); 39 | drawBar(&painter); 40 | drawHead(&painter); 41 | } 42 | 43 | void myWave1::drawBackground(QPainter *painter) 44 | { 45 | painter->save(); 46 | painter->setPen(Qt::NoPen); 47 | painter->setBrush(QColor(30,30,30)); 48 | painter->drawRect(rect()); 49 | painter->restore(); 50 | } 51 | 52 | void myWave1::drawBar(QPainter *painter) 53 | { 54 | painter->save(); 55 | painter->setRenderHints(QPainter::Antialiasing); 56 | painter->setRenderHints(QPainter::HighQualityAntialiasing); 57 | 58 | QPointF BarRectTopLeft(PYI_EXTRA_SPACE,m_barY); 59 | QPointF BarRectBottomRight(width() - PYI_EXTRA_SPACE,height()); 60 | QRectF BarRect(BarRectTopLeft,BarRectBottomRight); 61 | 62 | QLinearGradient BarGradient(BarRect.topLeft(),BarRect.topRight()); 63 | BarGradient.setColorAt(0.0,PYI_BAR_START_COLOR); 64 | BarGradient.setColorAt(0.5,PYI_BAR_END_COLOR); 65 | BarGradient.setColorAt(1.0,PYI_BAR_START_COLOR); 66 | 67 | painter->setPen(PYI_BAR_START_COLOR); 68 | painter->setBrush(BarGradient); 69 | painter->drawRect(BarRect); 70 | 71 | painter->restore(); 72 | } 73 | 74 | void myWave1::drawHead(QPainter *painter) 75 | { 76 | painter->save(); 77 | 78 | qreal Y = m_headY; 79 | 80 | QPointF HeadRectTopLeft(PYI_EXTRA_SPACE,Y); 81 | QPointF HeadRectBottomRight(width() - PYI_EXTRA_SPACE,Y - PYI_HEAD_HEIGHT) ; 82 | QRectF HeadRect(HeadRectTopLeft,HeadRectBottomRight); 83 | 84 | QLinearGradient HeadGradient(HeadRect.topLeft(),HeadRect.topRight()); 85 | HeadGradient.setColorAt(0.0,PYI_BAR_START_COLOR); 86 | HeadGradient.setColorAt(0.5,PYI_BAR_END_COLOR); 87 | HeadGradient.setColorAt(1.0,PYI_BAR_START_COLOR); 88 | 89 | painter->setPen(PYI_BAR_START_COLOR); 90 | painter->setBrush(HeadGradient); 91 | painter->drawRect(HeadRect); 92 | 93 | 94 | painter->restore(); 95 | } 96 | 97 | void myWave1::DoShrinkBar() 98 | { 99 | m_barY +=PYI_HEAD_Y_INCREMENT; 100 | update(); 101 | } 102 | 103 | void myWave1::DoDropHead() 104 | { 105 | m_headY +=PYI_HEAD_Y_INCREMENT; 106 | 107 | 108 | if(m_headY > height()) 109 | { 110 | if(m_dropTimer->isActive()) 111 | { 112 | m_dropTimer->stop(); 113 | } 114 | m_headY = height(); 115 | } 116 | 117 | update(); 118 | } 119 | 120 | void myWave1::setRange(int min, int max) 121 | { 122 | Q_ASSERT(min < max); 123 | m_nMin = min; 124 | m_nMax = max; 125 | update(); 126 | } 127 | 128 | void myWave1::setValue(int value) 129 | { 130 | m_nValue = value; 131 | m_nCurrentValue = value; 132 | 133 | int ValueDiff = m_nMax - m_nMin; 134 | 135 | if(ValueDiff == 0) 136 | { 137 | return ; 138 | } 139 | 140 | if(m_dropTimer->isActive()) 141 | { 142 | m_dropTimer->stop(); 143 | } 144 | 145 | qreal yIncre = (qreal)height()/ValueDiff; 146 | 147 | m_headY = height() - yIncre * m_nCurrentValue; 148 | m_barY = m_headY; 149 | 150 | update(); 151 | m_shrinkTimer->start(); 152 | m_toolTimer->start(); 153 | } 154 | 155 | int myWave1::minimum() const 156 | { 157 | return m_nMin; 158 | } 159 | 160 | int myWave1::maximum() const 161 | { 162 | return m_nMax; 163 | } 164 | 165 | int myWave1::value() const 166 | { 167 | return m_nValue; 168 | } 169 | -------------------------------------------------------------------------------- /mywave1.h: -------------------------------------------------------------------------------- 1 | #ifndef MYWAVE1_H 2 | #define MYWAVE1_H 3 | 4 | #define PYI_HEAD_HEIGHT 1 5 | #define PYI_BAR_START_COLOR QColor(88,177,245) 6 | #define PYI_BAR_END_COLOR QColor(74,149,206) 7 | #define PYI_HEAD_Y_INCREMENT 1 8 | #define PYI_VALUE_DECREMENT 1 9 | #define PYI_EXTRA_SPACE 2 10 | #define PYI_MAX_WIDTH 15 11 | 12 | #define DEBUG_VAR(VAR) qDebug() << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << #VAR << ":" << VAR; 13 | 14 | 15 | #include 16 | #include 17 | 18 | class myWave1 : public QWidget 19 | { 20 | Q_OBJECT 21 | public: 22 | explicit myWave1(QWidget *parent = 0); 23 | void setRange(int min,int max); 24 | 25 | void setValue(int value); 26 | 27 | int minimum() const; 28 | 29 | int maximum() const; 30 | 31 | int value() const; 32 | 33 | protected: 34 | void paintEvent(QPaintEvent *); 35 | 36 | QSize sizeHint() const 37 | { 38 | return QSize(20,200); 39 | } 40 | 41 | private: 42 | 43 | void drawBackground(QPainter* painter); 44 | 45 | void drawBar(QPainter* painter); 46 | 47 | void drawHead(QPainter* painter); 48 | 49 | 50 | private: 51 | int m_nMin; 52 | int m_nMax; 53 | int m_nValue; 54 | int m_nCurrentValue; 55 | 56 | qreal m_headY; 57 | qreal m_barY; 58 | 59 | QTimer* m_shrinkTimer; 60 | QTimer* m_dropTimer; 61 | QTimer* m_toolTimer; 62 | 63 | QRectF m_BarRect; 64 | 65 | private: 66 | void initVariables(); 67 | 68 | private slots: 69 | void DoShrinkBar(); 70 | void DoDropHead(); 71 | 72 | }; 73 | 74 | #endif // MYWAVE1_H 75 | -------------------------------------------------------------------------------- /mywave2.cpp: -------------------------------------------------------------------------------- 1 | #include "mywave2.h" 2 | #define BG2 3 | 4 | myWave2::myWave2(QWidget *parent) : 5 | QWidget(parent) 6 | { 7 | this->initVariables(); 8 | } 9 | 10 | void myWave2::initVariables() 11 | { 12 | m_nMin = 0; 13 | m_nMax = 0; 14 | m_nCurrentValue = 0; 15 | m_nValue = 0; 16 | m_nIncrement = 0; 17 | 18 | m_bReverse = false; 19 | 20 | m_updateTimer=new QTimer(this); 21 | m_updateTimer->setInterval(PYS_TIMER_INTERVAL); 22 | connect(m_updateTimer,SIGNAL(timeout()),this,SLOT(UpdateValue())); 23 | 24 | setMaximumWidth(PYS_WIDTH); 25 | } 26 | 27 | void myWave2::paintEvent(QPaintEvent *) 28 | { 29 | QPainter painter(this); 30 | drawBackground(&painter); 31 | drawSpectrum(&painter); 32 | } 33 | 34 | void myWave2::drawBackground(QPainter *painter) 35 | { 36 | 37 | #ifdef BG1 38 | painter->save(); 39 | painter->setPen(Qt::NoPen); 40 | 41 | QPointF TopRectTopLeft(0,0); 42 | QPointF TopRectBottomRight(width(),height()/2); 43 | QRectF TopRect(TopRectTopLeft,TopRectBottomRight); 44 | PYS_APPLY_LINEAR_GRADIENT(TopRect,TopRect.topLeft(),TopRect.bottomLeft(),PYS_BG_END_COLOR ,PYS_BG_START_COLOR); 45 | painter->drawRect(TopRect); 46 | 47 | QPointF BottomRectTopLeft(0,height()/2); 48 | QPointF BottomRectBottomRight(width(),height()); 49 | QRectF BottomRect(BottomRectTopLeft,BottomRectBottomRight); 50 | PYS_APPLY_LINEAR_GRADIENT(BottomRect,BottomRect.topLeft(),BottomRect.bottomLeft(),PYS_BG_START_COLOR,PYS_BG_END_COLOR) 51 | painter->drawRect(BottomRect); 52 | 53 | painter->restore(); 54 | #endif 55 | 56 | #ifdef BG2 57 | painter->save(); 58 | painter->setPen(QColor(50,50,50)); 59 | painter->setBrush(QColor(50,50,50)); 60 | painter->drawRect(rect()); 61 | painter->restore(); 62 | #endif 63 | 64 | #ifdef BG3 65 | #include 66 | QRadialGradient BgGradient(rect().center(),width() > height() ? height()/2:width(),rect().center()); 67 | BgGradient.setColorAt(0.0,QColor(156,156,156)); 68 | BgGradient.setColorAt(1.0,QColor(60,60,60)); 69 | painter->setBrush(BgGradient); 70 | painter->setPen(Qt::NoPen); 71 | painter->drawRect(rect()); 72 | #endif 73 | } 74 | 75 | void myWave2::drawSpectrum(QPainter *painter) 76 | { 77 | painter->save(); 78 | painter->setPen(Qt::NoPen); 79 | 80 | qreal CenterY = (qreal) rect().center().y(); 81 | 82 | 83 | qreal HalfHeight = (qreal) height()/2 - PYS_TOP_SPACE ; 84 | int Range = m_nMax - m_nMin; 85 | qreal YIncrement = (qreal)HalfHeight/Range; 86 | // draw top bar 87 | qreal TopRectY = YIncrement * m_nCurrentValue ; 88 | QPointF TopRectTopLeft(PYS_LEFT_SPACE, HalfHeight - TopRectY + PYS_TOP_SPACE ); 89 | QPointF TopRectBottomRight(width()- PYS_LEFT_SPACE,CenterY); 90 | QRectF TopRect(TopRectTopLeft,TopRectBottomRight); 91 | 92 | QLinearGradient TopRectGradient(TopRect.topLeft(),TopRect.topRight()); 93 | TopRectGradient.setColorAt(0.0,PYS_BAR_END_COLOR); 94 | TopRectGradient.setColorAt(0.5,PYS_BAR_START_COLOR); 95 | TopRectGradient.setColorAt(1.0,PYS_BAR_END_COLOR); 96 | painter->setBrush(TopRectGradient); 97 | 98 | painter->drawRect(TopRect); 99 | 100 | 101 | // draw bottom bar 102 | qreal BottomRectY = CenterY + YIncrement * m_nCurrentValue; 103 | QPointF BottomRectTopLeft(PYS_LEFT_SPACE,CenterY); 104 | QPointF BottomRectBottomRight(width() - PYS_LEFT_SPACE,BottomRectY); 105 | QRectF BottomRect(BottomRectTopLeft,BottomRectBottomRight); 106 | 107 | QLinearGradient BottomRectGradient(BottomRect.topLeft(),BottomRect.topRight()); 108 | BottomRectGradient.setColorAt(0.0,PYS_BAR_END_COLOR); 109 | BottomRectGradient.setColorAt(0.5,PYS_BAR_START_COLOR); 110 | BottomRectGradient.setColorAt(1.0,PYS_BAR_END_COLOR); 111 | painter->setBrush(BottomRectGradient); 112 | 113 | painter->drawRect(BottomRect); 114 | 115 | 116 | painter->restore(); 117 | } 118 | 119 | void myWave2::setRange(int min, int max) 120 | { 121 | m_nMin = min < max ? min : max; 122 | m_nMax = max > min ? max : min; 123 | 124 | m_nIncrement = ( m_nMax - m_nMin )/ PYS_INCREMENT_FACTOR; 125 | } 126 | 127 | void myWave2::setMinimum(int min) 128 | { 129 | m_nMin = min; 130 | } 131 | 132 | void myWave2::setMaximum(int max) 133 | { 134 | m_nMax = max; 135 | } 136 | 137 | void myWave2::setValue(int value) 138 | { 139 | if(m_nValue > value ) 140 | { 141 | m_bReverse = true; 142 | }else if(m_nValue < value ) 143 | { 144 | m_bReverse = false; 145 | }else{ 146 | STOP_TIMER(m_updateTimer); 147 | return ; 148 | } 149 | 150 | m_nValue = value; 151 | 152 | // DEBUG_VAR(m_nValue); 153 | 154 | STOP_TIMER(m_updateTimer); 155 | m_updateTimer->start(); 156 | } 157 | 158 | void myWave2::UpdateValue() 159 | { 160 | if(m_bReverse) 161 | { 162 | m_nCurrentValue -= m_nIncrement; 163 | 164 | if(m_nCurrentValue < m_nValue) 165 | { 166 | m_nCurrentValue = m_nValue; 167 | STOP_TIMER(m_updateTimer); 168 | } 169 | }else{ 170 | m_nCurrentValue += m_nIncrement; 171 | 172 | if(m_nCurrentValue > m_nValue) 173 | { 174 | m_nCurrentValue = m_nValue; 175 | STOP_TIMER(m_updateTimer); 176 | } 177 | } 178 | 179 | update(); 180 | } 181 | -------------------------------------------------------------------------------- /mywave2.h: -------------------------------------------------------------------------------- 1 | #ifndef MYWAVE2_H 2 | #define MYWAVE2_H 3 | 4 | #define PYS_TOP_SPACE 1 5 | #define PYS_LEFT_SPACE 0 6 | #define PYS_TIMER_INTERVAL 10 7 | #define PYS_WIDTH 3 8 | #define PYS_INCREMENT_FACTOR 100 9 | 10 | #define PYS_BG_START_COLOR QColor(178,178,178) 11 | #define PYS_BG_END_COLOR QColor(60,60,60) 12 | 13 | #define PYS_BAR_START_COLOR QColor(46,132,243) 14 | #define PYS_BAR_END_COLOR QColor(39,110,203) 15 | 16 | //#define PYS_BAR_START_COLOR QColor(157,157,235) 17 | //#define PYS_BAR_END_COLOR QColor(85,85,127) 18 | 19 | #define PYS_APPLY_LINEAR_GRADIENT(Rect,StartPot,EndPot,StartColor,EndColor) QLinearGradient Rect##Gradient(StartPot,EndPot); \ 20 | Rect##Gradient.setColorAt(0.0,StartColor); \ 21 | Rect##Gradient.setColorAt(1.0,EndColor); \ 22 | painter->setBrush(Rect##Gradient); 23 | 24 | #ifdef DEBUG_VAR 25 | #undef DEBUG_VAR 26 | #endif 27 | #include 28 | #define DEBUG_VAR(Var) qDebug() << "["<<__FILE__ <<"]" << "Line:" << __LINE__ << __FUNCTION__ << #Var << "=" << Var; 29 | 30 | 31 | #ifdef STOP_TIMER 32 | #undef STOP_TIMER 33 | #endif 34 | #define STOP_TIMER(Timer) if(Timer->isActive()){ Timer->stop();} 35 | 36 | 37 | #include 38 | #include 39 | 40 | class myWave2 : public QWidget 41 | { 42 | Q_OBJECT 43 | public: 44 | explicit myWave2(QWidget *parent = 0); 45 | 46 | public: 47 | 48 | void setRange(int min,int max); 49 | 50 | void setMinimum(int min); 51 | 52 | void setMaximum(int max); 53 | 54 | public slots: 55 | void setValue(int value); 56 | 57 | protected: 58 | void paintEvent(QPaintEvent *); 59 | 60 | QSize sizeHint() const 61 | { 62 | return QSize(PYS_WIDTH,120); 63 | } 64 | 65 | private: 66 | void drawBackground(QPainter* painter); 67 | void drawSpectrum(QPainter* painter); 68 | 69 | private: 70 | void initVariables(); 71 | 72 | private: 73 | int m_nMin; 74 | int m_nMax; 75 | int m_nCurrentValue; 76 | int m_nValue; 77 | int m_nIncrement; 78 | 79 | bool m_bReverse; 80 | 81 | QTimer* m_updateTimer; 82 | 83 | private slots: 84 | void UpdateValue(); 85 | 86 | }; 87 | 88 | #endif // MYWAVE2_H 89 | --------------------------------------------------------------------------------