AlbumShaper 1.0a3
guiTools.cpp
Go to the documentation of this file.
1//==============================================
2// copyright : (C) 2003-2005 by Will Stokes
3//==============================================
4// This program is free software; you can redistribute it
5// and/or modify it under the terms of the GNU General
6// Public License as published by the Free Software
7// Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//==============================================
10
11//Systemwide includes
12#include <qwidget.h>
13#include <qapplication.h>
14#include <qstring.h>
15#include <qstringlist.h>
16#include <qpixmap.h>
17#include <QDesktopWidget>
18
19//Projectwide includes
20#include "guiTools.h"
21
22#define SUBALBUM_TEXT_LENGTH 35
23#define PHOTO_TEXT_LENGTH 35
24
25//==============================================
26void centerWindow(QWidget* window)
27{
28 //---------------------------------------------------------
29 //get size and location of application window
30 QRect appRec = qApp->mainWidget()->frameGeometry();
31 QRect windowRec = window->frameGeometry();
32 //---------------------------------------------------------
33 //if new window smaller then application window then center window
34 //over application window, otherwise align left/top with application window
35 int x, y;
36
37 //horizontal alignment
38 if(windowRec.width() < appRec.width())
39 { x = appRec.x() + ((appRec.width() - windowRec.width())/2); }
40 else
41 { x = appRec.x(); }
42
43 //vertical alignment
44 if(windowRec.height() < appRec.height())
45 { y = appRec.y() + ((appRec.height() - windowRec.height())/2); }
46 else
47 { y = appRec.y(); }
48 //---------------------------------------------------------
49 //ensure window is not off screen, favor top/left sides of window is bigger than screen!
50 QRect screen = QApplication::desktop()->availableGeometry();
51
52 //right
53 if(x + windowRec.width() > screen.width() )
54 x = screen.width() - windowRec.width();
55
56 //left
57 if(x < 0)
58 x = 0;
59
60 //bottom
61 if(y + windowRec.height() > screen.height() )
62 y = screen.height() - windowRec.height();
63
64 //top
65 if(y < 0)
66 y = 0;
67 //---------------------------------------------------------
68 //place window
69 window->move( QPoint( x, y) );
70}
71//==============================================
72QString clipText(QString string, int lines, int lineWidth)
73{
74 if(lineWidth == 0)
75 {
76// cout << "ERROR: given 0 width when clipping: " << string << endl;
77 return "";
78 }
79
80 QString result = "";
81 QString building = "";
82 QFontMetrics fm( qApp->font() );
83
84 //decrement characters off head of string for each line
85 while(string.length() > 0 && lines > 0)
86 {
87 bool spaceFound = false;
88 QString line = "";
89
90 //while there are character to be popped up
91 while(string.length() > 0)
92 {
93 //if we can afford to add this character, add it to the building string
94 //then update the character found, and space found strings
95 if(fm.width( QString(line + building + string.at(0) ) ) < lineWidth )
96 {
97 building = building + string.at(0);
98
99 //found a space, add this space and all built up words to the text for this line, no need
100 //to wrap what has been found so far
101 if(string.at(0) == ' ') // QChar::Separator_Space)
102 {
103 line = line + building;
104 building = "";
105 spaceFound = true;
106 string = string.remove(0, 1);
107 continue;
108 }
109
110 string = string.remove(0, 1);
111 if(string.length() == 0)
112 {
113 line = line + building;
114 building = "";
115 }
116
117 }
118 //uh oh, can't add this character? move to next line
119 else
120 {
121 //if no spaces found up to this point, suck up character so far, we're breaking on this one
122 if(!spaceFound || lines == 1)
123 {
124 if(lines == 1)
125 building = building + string;
126
127 //if this is the last line then make sure we have enough space for the ...
128 line = line + building;
129 if(fm.width( line ) > lineWidth )
130 {
131 while( fm.width(line + "...") > lineWidth )
132 {
133 line.truncate( line.length() - 1);
134 }
135 line = line + "...";
136 }
137 building = "";
138 }
139 break;
140 }
141 }
142
143 //move on to next line
144 result = result + line;
145 line = "";
146 lines--;
147 }
148
149 return result;
150}
151//==============================================
152QString clipPhotoText(const QString in)
153{
154 if(in.length() > PHOTO_TEXT_LENGTH)
155 {
156 QString res = in;
157 res.truncate(PHOTO_TEXT_LENGTH-3); res = res + "...";
158 return res;
159 }
160 else
161 return in;
162}
163//==============================================
#define PHOTO_TEXT_LENGTH
Definition guiTools.cpp:23
void centerWindow(QWidget *window)
Definition guiTools.cpp:26
QString clipText(QString string, int lines, int lineWidth)
clip text to fit within numer of lines and max width
Definition guiTools.cpp:72
QString clipPhotoText(const QString in)
clip photo text
Definition guiTools.cpp:152