-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathpngyu_util.h
235 lines (199 loc) · 5.47 KB
/
pngyu_util.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifndef PNGYU_UTIL_H
#define PNGYU_UTIL_H
#include <cmath>
#include <QWidget>
#include <QApplication>
#include <QCursor>
#include <QFileInfo>
#include <QImageReader>
#include <QRegExp>
#include <QDir>
#include <QFile>
#include <QByteArray>
#include <QStringList>
#include <QProcess>
#include <QIcon>
namespace pngyu
{
namespace util
{
namespace detail
{
const QColor DEFAULT_ENABLE_COLOR = QColor("lightskyblue");
}
inline QString app_home_path()
{
return QDir::homePath() + "/.pngyu";
}
inline QString app_temporay_path()
{
return app_home_path() + "/temp";
}
inline bool ma_app_home_path()
{
return QDir().mkpath( app_home_path() );
}
inline bool make_app_temporary_path()
{
return QDir().mkpath( app_temporay_path() );
}
inline QByteArray png_file_to_bytearray( const QString &filename )
{
QFile f( filename );
f.open( QIODevice::ReadOnly );
return f.readAll();
}
inline bool write_png_data( const QString &filename, const QByteArray png_data )
{
QFile f( filename );
if( ! f.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
return false;
}
if( f.write( png_data ) != png_data.size() )
{
return false;
}
return true;
}
inline QString size_to_string_kb( const qint64 size )
{
return QString( "%1KB" )
.arg( static_cast<double>(size) / 1024, 0, 'f', 2 );
}
inline QString size_to_string_mb( const qint64 size )
{
return QString( "%1MB" )
.arg( static_cast<double>(size) / ( 1024 * 1024 ), 0, 'f', 2 );
}
inline bool has_png_extention( const QFileInfo &file )
{
return QRegExp( "png", Qt::CaseInsensitive ).exactMatch( file.suffix() );
}
inline bool can_read_png_file( const QFileInfo &file )
{
QImageReader image_reader( file.absoluteFilePath(), "png" );
return image_reader.canRead();
}
inline QImage read_thumbnail_image( const QString &filename, const int size )
{
QImageReader image_reader( filename );
const QSize &origin_size = image_reader.size();
const double scale = static_cast<double>( size ) / std::max( origin_size.width(), origin_size.height() );
const QSize dst_size( scale * origin_size.width(), scale * origin_size.height() );
image_reader.setScaledSize( dst_size );
return image_reader.read();
}
inline bool is_under_mouse( const QWidget * const widget )
{
return QRect( QPoint(), widget->size() ).contains( widget->mapFromGlobal( QCursor::pos() ) );
}
inline void set_drop_enabled_palette(
QWidget * const widget,
const bool enabled,
const QColor &enable_color = detail::DEFAULT_ENABLE_COLOR )
{
QPalette palette = widget->palette();
if( enabled )
{
palette.setBrush( QPalette::Base, QBrush( enable_color ) );
}
else
{
palette.setBrush( QPalette::Base, QPalette().brush( QPalette::Base ) );
}
widget->setPalette( palette );
}
inline void set_drop_here_stylesheet(
QWidget * const widget,
const bool drag_hoverring,
const QColor &hoverring_color = detail::DEFAULT_ENABLE_COLOR )
{
// widget->setStyleSheet();
QString stylesheet =
"QWidget{"
"background-image : url(:/background/drop_here.png);"
"background-position: center ;"
"background-repeat : repeat-none;";
if( drag_hoverring )
{
stylesheet += "background-color : " + hoverring_color.name() + ";\n";
}
else
{
stylesheet += "background-color : " + QColor(Qt::white).name() + ";\n";
}
stylesheet += "}";
widget->setStyleSheet( stylesheet );
}
inline const QPixmap& success_icon_pixmap()
{
static QPixmap p( ":/icons/check.png" );
return p;
}
inline const QPixmap& failure_icon_pixmap()
{
static QPixmap p( ":/icons/stop.png" );
return p;
}
inline const QIcon& success_icon()
{
static QIcon i( success_icon_pixmap() );
return i;
}
inline const QIcon& failure_icon()
{
static QIcon i( failure_icon_pixmap() );
return i;
}
inline bool open_with_mac_app( const QStringList &files, const QString app_path )
{
// QStringList args;
// args.push_back( "-c" ); // python option
// { // make python script
// QString script;
// script += "# -*- coding: utf-8-mac -*-\n"
// "import AppKit\n"
// "w = AppKit.NSWorkspace.sharedWorkspace()\n";
// foreach( const QString &filepath, files )
// {
// script += QString("w.openFile_withApplication_(u'%1', '%2')\n" ).arg( filepath, app_path );
// }
// args.push_back( script );
// }
// return QProcess::execute( "/usr/bin/pythonw", args );
return QProcess::startDetached( "/usr/bin/open", (QStringList() << "-a" << app_path) += files );
//return QProcess::startDetached( app_path, files );
//return QProcess::execute( app_path, files );
}
inline const QString& dot_path()
{
static QString p = QFileInfo( QApplication::applicationDirPath() ).absoluteFilePath();
return p;
}
inline const QString& dot_dot_path()
{
static QString p = QFileInfo( dot_path() + "/.." ).absoluteFilePath();
return p;
}
inline QString to_dot_path( const QString &path )
{
const QString &dot = dot_path();
const QString &dot_dot = dot_dot_path();
QString dot_path = QFileInfo( path ).absoluteFilePath();
dot_path.replace( dot, "." );
dot_path.replace( dot_dot, ".." );
return dot_path;
}
inline QString from_dot_path( const QString &path )
{
const QString &dot = QFileInfo( QApplication::applicationDirPath() ).absoluteFilePath();
const QString &dot_dot = QFileInfo( dot_path() + "/.." ).absoluteFilePath();
QString abs_path = path;
abs_path.replace( "../", dot_dot + "/" );
abs_path.replace( "./", dot + "/" );
return abs_path;
}
} // namespace util
} // namespace pngyu
#endif // PNGYU_UTIL_H