-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwidget.cpp
executable file
·180 lines (145 loc) · 4.83 KB
/
widget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "widget.h"
#include <QDesktopServices>
#include <QMessageBox>
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
//ui->setupUi(this);
this->setWindowOpacity(0.5);
this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
this->setMouseTracking(true);
this->setAttribute(Qt::WA_TranslucentBackground);
this->setWindowTitle("IRuler by Awesomez V1.2");
this->setFixedSize(600,120);
createContentMenu();
}
Widget::~Widget()
{
//delete ui;
}
void Widget::mousePressEvent(QMouseEvent * event){
if (event->button() == Qt::LeftButton){
if(event->x()>this->width()-5){
changeWidth=true;
}else{
changeWidth=false;
}
if(event->y()>this->height()-5){
changeHeight=true;
}else{
changeHeight=false;
}
dragPosition = event->globalPos() - frameGeometry().topLeft();
event->accept();
}else if(event->button() == Qt::RightButton){
contentMenu->exec(this->cursor().pos());
}
}
void Widget::mouseMoveEvent(QMouseEvent * event){
if(event->x()>this->width()-5){
this->setCursor(Qt::SizeHorCursor);
}else if(event->y()>this->height()-5){
this->setCursor(Qt::SizeVerCursor);
}else{
this->setCursor(Qt::ArrowCursor);
}
if (event->buttons() == Qt::LeftButton){
if((changeWidth&&!changeHeight)){
int thisx=event->pos().x();
if(thisx>30){
this->setFixedWidth(thisx);
}
}else if(!changeWidth&&changeHeight){
int thisy=event->pos().y();
if(thisy>30){
this->setFixedHeight(thisy);
}
}else{
move(event->globalPos()-dragPosition);
event->accept();
}
}
}
void Widget::paintEvent(QPaintEvent *){
QPainter p(this);
p.fillRect(rect(), QColor(255,255,255,150));
drawXLine();
drawYLine();
int iwidth=this->width();
int iheight=this->height();
QPainter p2(this);
p2.setPen(QPen(QColor("#ddd"), 1, Qt::SolidLine));
p2.drawRect(0,0,iwidth-1,iheight-1);
QString showText="W:"+QString::number(iwidth)+"px\r\nH:"+QString::number(iheight)+"px";
p2.setPen(QPen(QColor("#940080"), 1, Qt::SolidLine));
p2.setFont(QFont("Arial", 10));
p2.drawText(this->rect(), Qt::AlignCenter,showText);
}
void Widget::drawXLine(){
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing, false);
p.setPen(QPen(QColor("#000"), 1, Qt::SolidLine));
int lineHeight = 5;
int iwidth=this->width();
for (int i = 0; i < iwidth; i+=2) {
lineHeight = 5;
if(i % 10 == 0){
lineHeight = 10;
}
if (i % 50 == 0 && i>0) {
p.setFont(QFont("Arial", 7, QFont::Bold));
p.drawText( i, 20, 25, 10, Qt::AlignLeft, QString::number(i));
lineHeight = 15;
}
p.drawLine(QPoint(i, 0), QPoint(i, lineHeight));
}
}
void Widget::drawYLine(){
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing, false);
p.setPen(QPen(QColor("#000"), 1, Qt::SolidLine));
int lineHeight = 5;
int iheight=this->height();
for (int i = 0; i <iheight; i+=2) {
lineHeight = 5;
if(i % 10 == 0){
lineHeight = 10;
}
if (i % 50 == 0 && i>0) {
p.setFont(QFont("Arial", 7, QFont::Bold));
p.drawText(20, i, 25, 10, Qt::AlignLeft, QString::number(i));
lineHeight = 15;
}
p.drawLine(QPoint(0, i), QPoint(lineHeight, i));
}
}
void Widget::createContentMenu(){
QAction *Tray_quit = new QAction("Quit", this);
//Tray_quit->setIcon(QIcon(":/image/image/delete.png"));
connect(Tray_quit, SIGNAL(triggered(bool)), this, SLOT(slotQuit()));
QAction *Tray_homepage = new QAction("Homepage", this);
connect(Tray_homepage, SIGNAL(triggered(bool)), this, SLOT(slotHomepage()));
QAction *Tray_changelog = new QAction("Changelog", this);
connect(Tray_changelog, SIGNAL(triggered(bool)), this, SLOT(slotChangelog()));
QAction *menuAbout = new QAction("About", this);
connect(menuAbout, SIGNAL(triggered(bool)), this, SLOT(soltAbout()));
contentMenu = new QMenu(this);
contentMenu->addAction(Tray_homepage);
contentMenu->addAction(Tray_changelog);
contentMenu->addAction(menuAbout);
contentMenu->addSeparator();
contentMenu->addAction(Tray_quit);
}
void Widget::slotQuit(){
this->close();
// QApplication::quit();
}
void Widget::slotHomepage(){
QDesktopServices::openUrl(QUrl("https://github.com/Awesomez-Qt/IRuler"));
}
void Widget::slotChangelog(){
QDesktopServices::openUrl(QUrl("https://github.com/Awesomez-Qt/IRuler/commits/master"));
}
void Widget::soltAbout(){
QMessageBox::information(this,"About","IRuler by Awesomez V1.2.20130814",QMessageBox::Yes);
}