-
Notifications
You must be signed in to change notification settings - Fork 4
/
ninepatch.h
93 lines (83 loc) · 2.61 KB
/
ninepatch.h
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
#pragma once
#include <QImage>
#include <QPainter>
#include <QString>
#include <exception>
#include <string>
class TNinePatch {
public:
TNinePatch(QImage& image);
void Draw(QPainter& painter, int x, int y);
void SetImageSize(int width, int height);
~TNinePatch();
QRect GetContentArea(int widht, int height);
private:
QRect GetContentArea();
void GetResizeArea();
void GetFactor(int width, int height, double& factorX, double& factorY);
void UpdateCachedImage(int width, int height);
void DrawScaledPart(QRect oldRect, QRect newRect, QPainter& painter);
void DrawConstPart(QRect oldRect, QRect newRect, QPainter& painter);
public:
QImage& Image;
private:
int OldWidth = -1;
int OldHeight = -1;
QImage CachedImage;
QVector<std::pair< int, int >>ResizeDistancesY;
QVector<std::pair< int, int >>ResizeDistancesX;
QRect ContentArea;
};
class NinePatchException : public std::exception {
public:
virtual const char* what() const throw() override {
return "Nine patch error";
}
};
class ExceptionIncorrectWidth : public NinePatchException {
public:
ExceptionIncorrectWidth(int imgW, int imgH) {
ImgWidth = imgW;
ImgHeight = imgH;
}
virtual const char* what() const throw() override {
std::string str = ("Input incorrect width. Mimimum width = " + std::to_string(ImgWidth));
return str.c_str();
}
public:
int ImgWidth;
int ImgHeight;
};
class ExceptionIncorrectWidthAndHeight : public NinePatchException {
public:
ExceptionIncorrectWidthAndHeight(int imgW,int imgH) {
ImgWidth = imgW;
ImgHeight = imgH;
}
virtual const char* what() const throw() override {
std::string str = ("Input incorrect width width and height. Minimum width = " + std::to_string(ImgWidth)+ ". Minimum height = " + std::to_string(ImgHeight));
return str.c_str();
}
public:
int ImgWidth;
int ImgHeight;
};
class ExceptionIncorrectHeight : public NinePatchException {
public:
ExceptionIncorrectHeight(int imgW, int imgH) {
ImgWidth = imgW;
ImgHeight = imgH;
}
virtual const char* what() const throw() override {
std::string str = ("Input incorrect heigh. Minimum height = " + std::to_string(ImgHeight)) ;
return str.c_str();
}
public:
int ImgWidth;
int ImgHeight;
};
class ExceptionNot9Patch : public NinePatchException {
virtual const char* what() const throw() override {
return "It is not nine patch image";
}
};