guiTools.cpp

Go to the documentation of this file.
00001 //==============================================
00002 //  copyright            : (C) 2003-2005 by Will Stokes
00003 //==============================================
00004 //  This program is free software; you can redistribute it
00005 //  and/or modify it under the terms of the GNU General
00006 //  Public License as published by the Free Software
00007 //  Foundation; either version 2 of the License, or
00008 //  (at your option) any later version.
00009 //==============================================
00010 
00011 //Systemwide includes
00012 #include <qwidget.h>
00013 #include <qapplication.h>
00014 #include <qstring.h>
00015 #include <qstringlist.h>
00016 #include <qpixmap.h>
00017 
00018 //Projectwide includes
00019 #include "guiTools.h"
00020 
00021 #define SUBALBUM_TEXT_LENGTH 35
00022 #define PHOTO_TEXT_LENGTH  35
00023 
00024 //==============================================
00025 void centerWindow(QWidget* window)
00026 {
00027   //---------------------------------------------------------
00028   //get size and location of application window
00029   QRect appRec = qApp->mainWidget()->frameGeometry();
00030   QRect windowRec = window->frameGeometry();
00031   //---------------------------------------------------------
00032   //if new window smaller then application window then center window
00033   //over application window, otherwise align left/top with application window
00034   int x, y;
00035 
00036   //horizontal alignment
00037   if(windowRec.width() < appRec.width())
00038   { x = appRec.x() + ((appRec.width() - windowRec.width())/2); }
00039   else
00040   { x = appRec.x(); }
00041 
00042   //vertical alignment
00043   if(windowRec.height() < appRec.height())
00044   { y = appRec.y() + ((appRec.height() - windowRec.height())/2); }
00045   else
00046   { y = appRec.y(); }
00047   //---------------------------------------------------------
00048   //ensure window is not off screen, favor top/left sides of window is bigger than screen!
00049   QRect screen = QApplication::desktop()->availableGeometry();
00050 
00051   //right
00052   if(x + windowRec.width() > screen.width() )
00053     x = screen.width() - windowRec.width();
00054 
00055   //left
00056   if(x < 0)
00057     x = 0;
00058 
00059   //bottom
00060   if(y + windowRec.height() > screen.height() )
00061     y = screen.height() - windowRec.height();
00062 
00063   //top
00064   if(y < 0)
00065     y = 0;
00066   //---------------------------------------------------------
00067   //place window
00068   window->move( QPoint( x, y) );
00069 }
00070 //==============================================
00071 QString clipText(QString string, int lines, int lineWidth)
00072 {
00073   if(lineWidth == 0)
00074   {
00075 //    cout << "ERROR: given 0 width when clipping: " << string << endl;
00076     return "";
00077   }
00078 
00079   QString result = "";
00080   QString building = "";
00081   QFontMetrics fm( qApp->font() );
00082 
00083   //decrement characters off head of string for each line
00084   while(string.length() > 0 && lines > 0)
00085   {
00086     bool spaceFound = false;
00087     QString line = "";
00088 
00089     //while there are character to be popped up
00090     while(string.length() > 0)
00091     {
00092       //if we can afford to add this character, add it to the building string
00093       //then update the character found, and space found strings
00094       if(fm.width( QString(line + building + string.at(0) ) )  < lineWidth )
00095       {
00096         building = building + string.at(0);
00097 
00098         //found a space, add this space and all built up words to the text for this line, no need
00099         //to wrap what has been found so far
00100         if(string.at(0) == ' ') // QChar::Separator_Space)
00101         {
00102           line = line + building;
00103           building = "";
00104           spaceFound = true;
00105           string = string.remove(0, 1);
00106           continue;
00107         }
00108 
00109         string = string.remove(0, 1);
00110         if(string.length() == 0)
00111         {
00112           line = line + building;
00113           building = "";
00114         }
00115 
00116       }
00117       //uh oh, can't add this character? move to next line
00118       else
00119       {
00120         //if no spaces found up to this point, suck up character so far, we're breaking on this one
00121         if(!spaceFound || lines == 1)
00122         {
00123           if(lines == 1)
00124             building = building + string;
00125 
00126           //if this is the last line then make sure we have enough space for the ...
00127           line = line + building;
00128           if(fm.width( line ) >  lineWidth )
00129           {
00130             while( fm.width(line + "...") > lineWidth )
00131             {
00132               line.truncate( line.length() - 1);
00133             }
00134             line = line + "...";
00135           }
00136           building = "";
00137         }
00138         break;
00139       }
00140     }
00141 
00142     //move on to next line
00143     result = result + line;
00144     line = "";
00145     lines--;
00146   }
00147 
00148   return result;
00149 }
00150 //==============================================
00151 QString clipPhotoText(const QString in)
00152 {
00153   if(in.length() > PHOTO_TEXT_LENGTH)
00154   {
00155     QString res = in;
00156     res.truncate(PHOTO_TEXT_LENGTH-3); res = res + "...";
00157     return res;
00158   }
00159   else
00160     return in;
00161 }
00162 //==============================================

Generated on Wed Jan 24 05:38:28 2007 for AlbumShaper by  doxygen 1.5.1