AlbumShaper 1.0a3
guiTools.cpp File Reference
#include <qwidget.h>
#include <qapplication.h>
#include <qstring.h>
#include <qstringlist.h>
#include <qpixmap.h>
#include <QDesktopWidget>
#include "guiTools.h"
Include dependency graph for guiTools.cpp:

Go to the source code of this file.

Macros

#define SUBALBUM_TEXT_LENGTH   35
 
#define PHOTO_TEXT_LENGTH   35
 

Functions

void centerWindow (QWidget *window)
 
QString clipText (QString string, int lines, int lineWidth)
 clip text to fit within numer of lines and max width
 
QString clipPhotoText (const QString in)
 clip photo text
 

Macro Definition Documentation

◆ PHOTO_TEXT_LENGTH

#define PHOTO_TEXT_LENGTH   35

Definition at line 23 of file guiTools.cpp.

Referenced by clipPhotoText().

◆ SUBALBUM_TEXT_LENGTH

#define SUBALBUM_TEXT_LENGTH   35

Definition at line 22 of file guiTools.cpp.

Function Documentation

◆ centerWindow()

void centerWindow ( QWidget * window)

Definition at line 26 of file guiTools.cpp.

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}

Referenced by TitleWidget::aboutProgram(), TitleWidget::albumStatistics(), TitleWidget::help(), main(), and TitleWidget::settings().

◆ clipPhotoText()

QString clipPhotoText ( const QString in)

clip photo text

Definition at line 152 of file guiTools.cpp.

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}
#define PHOTO_TEXT_LENGTH
Definition guiTools.cpp:23

References PHOTO_TEXT_LENGTH.

◆ clipText()

QString clipText ( QString string,
int lines,
int lineWidth )

clip text to fit within numer of lines and max width

Definition at line 72 of file guiTools.cpp.

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}

Referenced by PhotoPreviewWidget::setText(), and SubalbumPreviewWidget::setText().