korganizer

timelabels.cpp

00001 /*
00002     This file is part of KOrganizer.
00003     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00004     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include "timelabels.h"
00026 
00027 #include <qhbox.h>
00028 #include <qvbox.h>
00029 #include <qlabel.h>
00030 #include <qframe.h>
00031 #include <qlayout.h>
00032 #include <qfont.h>
00033 #include <qfontmetrics.h>
00034 #include <qpainter.h>
00035 #include <qstringlist.h>
00036 #include <qdatetime.h>
00037 
00038 #include <kglobal.h>
00039 
00040 #include "koglobals.h"
00041 #include "kocore.h"
00042 #include "koprefs.h"
00043 #include "koagenda.h"
00044 
00045 TimeLabels::TimeLabels(int rows,QWidget *parent,const char *name,WFlags f) :
00046   QScrollView(parent,name,f)
00047 {
00048   mRows = rows;
00049   mMiniWidth = 0;
00050 
00051   mCellHeight = KOPrefs::instance()->mHourSize*4;
00052 
00053   enableClipper(true);
00054 
00055   setHScrollBarMode(AlwaysOff);
00056   setVScrollBarMode(AlwaysOff);
00057 
00058   resizeContents(50, int(mRows * mCellHeight) );
00059 
00060   viewport()->setBackgroundMode( PaletteBackground );
00061 
00062   mMousePos = new QFrame(this);
00063   mMousePos->setLineWidth(0);
00064   mMousePos->setMargin(0);
00065   mMousePos->setBackgroundColor(Qt::red);
00066   mMousePos->setFixedSize(width(), 1);
00067   addChild(mMousePos, 0, 0);
00068 }
00069 
00070 void TimeLabels::mousePosChanged(const QPoint &pos)
00071 {
00072   moveChild(mMousePos, 0, pos.y());
00073 }
00074 
00075 void TimeLabels::showMousePos()
00076 {
00077   mMousePos->show();
00078 }
00079 
00080 void TimeLabels::hideMousePos()
00081 {
00082   mMousePos->hide();
00083 }
00084 
00085 void TimeLabels::setCellHeight(double height)
00086 {
00087   mCellHeight = height;
00088 }
00089 
00090 /*
00091   Optimization so that only the "dirty" portion of the scroll view
00092   is redrawn.  Unfortunately, this is not called by default paintEvent() method.
00093 */
00094 void TimeLabels::drawContents(QPainter *p,int cx, int cy, int cw, int ch)
00095 {
00096   // bug:  the parameters cx and cw are the areas that need to be
00097   //       redrawn, not the area of the widget.  unfortunately, this
00098   //       code assumes the latter...
00099 
00100   // now, for a workaround...
00101   cx = contentsX() + frameWidth()*2;
00102   cw = contentsWidth() ;
00103   // end of workaround
00104 
00105   int cell = ((int)(cy/mCellHeight));
00106   double y = cell * mCellHeight;
00107   QFontMetrics fm = fontMetrics();
00108   QString hour;
00109   QString suffix = "am";
00110   int timeHeight =  fm.ascent();
00111   QFont nFont = font();
00112   p->setFont( font() );
00113 
00114   if (!KGlobal::locale()->use12Clock()) {
00115       suffix = "00";
00116   } else
00117       if (cell > 11) suffix = "pm";
00118 
00119   if ( timeHeight >  mCellHeight ) {
00120     timeHeight = int(mCellHeight-1);
00121     int pointS = nFont.pointSize();
00122     while ( pointS > 4 ) {
00123       nFont.setPointSize( pointS );
00124       fm = QFontMetrics( nFont );
00125       if ( fm.ascent() < mCellHeight )
00126         break;
00127       -- pointS;
00128     }
00129     fm = QFontMetrics( nFont );
00130     timeHeight = fm.ascent();
00131   }
00132   //timeHeight -= (timeHeight/4-2);
00133   QFont sFont = nFont;
00134   sFont.setPointSize( sFont.pointSize()/2 );
00135   QFontMetrics fmS(  sFont );
00136   int startW = mMiniWidth - frameWidth()-2 ;
00137   int tw2 = fmS.width(suffix);
00138   int divTimeHeight = (timeHeight-1) /2 - 1;
00139   //testline
00140   //p->drawLine(0,0,0,contentsHeight());
00141   while (y < cy + ch+mCellHeight) {
00142     // hour, full line
00143     p->drawLine( cx, int(y), cw+2, int(y) );
00144     hour.setNum(cell);
00145     // handle 24h and am/pm time formats
00146     if (KGlobal::locale()->use12Clock()) {
00147       if (cell == 12) suffix = "pm";
00148       if (cell == 0) hour.setNum(12);
00149       if (cell > 12) hour.setNum(cell - 12);
00150     }
00151 
00152     // center and draw the time label
00153     int timeWidth = fm.width(hour);
00154     int offset = startW - timeWidth - tw2 -1 ;
00155     p->setFont( nFont );
00156     p->drawText( offset, int(y+timeHeight), hour);
00157     p->setFont( sFont );
00158     offset = startW - tw2;
00159     p->drawText( offset, int(y+timeHeight-divTimeHeight), suffix);
00160 
00161     // increment indices
00162     y += mCellHeight;
00163     cell++;
00164   }
00165 
00166 }
00167 
00171 int TimeLabels::minimumWidth() const
00172 {
00173   return mMiniWidth;
00174 }
00175 
00177 void TimeLabels::updateConfig()
00178 {
00179   setFont(KOPrefs::instance()->mTimeBarFont);
00180 
00181   QString test = "20";
00182   if ( KGlobal::locale()->use12Clock() )
00183       test = "12";
00184   mMiniWidth = fontMetrics().width( test );
00185   if ( KGlobal::locale()->use12Clock() )
00186       test = "pm";
00187   else {
00188       test = "00";
00189   }
00190   QFont sFont = font();
00191   sFont.setPointSize(  sFont.pointSize()/2 );
00192   QFontMetrics fmS(   sFont );
00193   mMiniWidth += fmS.width(  test ) + frameWidth()*2+4 ;
00194   // update geometry restrictions based on new settings
00195   setFixedWidth(  mMiniWidth );
00196 
00197   // update HourSize
00198   mCellHeight = KOPrefs::instance()->mHourSize*4;
00199   // If the agenda is zoomed out so that more then 24 would be shown,
00200   // the agenda only shows 24 hours, so we need to take the cell height
00201   // from the agenda, which is larger than the configured one!
00202   if ( mCellHeight < 4*mAgenda->gridSpacingY() )
00203        mCellHeight = 4*mAgenda->gridSpacingY();
00204   resizeContents( mMiniWidth, int(mRows * mCellHeight+1) );
00205 }
00206 
00208 void TimeLabels::positionChanged()
00209 {
00210   int adjustment = mAgenda->contentsY();
00211   setContentsPos(0, adjustment);
00212 }
00213 
00214 void TimeLabels::positionChanged( int pos )
00215 {
00216   setContentsPos( 0, pos );
00217 }
00218 
00220 void TimeLabels::setAgenda(KOAgenda* agenda)
00221 {
00222   mAgenda = agenda;
00223 
00224   connect(mAgenda, SIGNAL(mousePosSignal(const QPoint &)), this, SLOT(mousePosChanged(const QPoint &)));
00225   connect(mAgenda, SIGNAL(enterAgenda()), this, SLOT(showMousePos()));
00226   connect(mAgenda, SIGNAL(leaveAgenda()), this, SLOT(hideMousePos()));
00227   connect(mAgenda, SIGNAL(gridSpacingYChanged( double ) ), this, SLOT( setCellHeight( double ) ) );
00228 }
00229 
00230 
00232 void TimeLabels::paintEvent(QPaintEvent*)
00233 {
00234 //  kdDebug(5850) << "paintevent..." << endl;
00235   // this is another hack!
00236 //  QPainter painter(this);
00237   //QString c
00238   repaintContents(contentsX(), contentsY(), visibleWidth(), visibleHeight());
00239 }
00240 
00241 #include "timelabels.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys