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 TQt, and distribute the resulting executable, 00022 without including the source code for TQt in the source distribution. 00023 */ 00024 00025 #include "timelabels.h" 00026 00027 #include <tqhbox.h> 00028 #include <tqvbox.h> 00029 #include <tqlabel.h> 00030 #include <tqframe.h> 00031 #include <tqlayout.h> 00032 #include <tqfont.h> 00033 #include <tqfontmetrics.h> 00034 #include <tqpainter.h> 00035 #include <tqstringlist.h> 00036 #include <tqdatetime.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,TQWidget *parent,const char *name,WFlags f) : 00046 TQScrollView(parent,name,f) 00047 { 00048 mRows = rows; 00049 mMiniWidth = 0; 00050 mAgenda = 0; 00051 00052 mCellHeight = KOPrefs::instance()->mHourSize*4; 00053 00054 enableClipper(true); 00055 00056 setHScrollBarMode(AlwaysOff); 00057 setVScrollBarMode(AlwaysOff); 00058 00059 resizeContents(50, int(mRows * mCellHeight) ); 00060 00061 viewport()->setBackgroundMode( PaletteBackground ); 00062 00063 mMousePos = new TQFrame(this); 00064 mMousePos->setLineWidth(0); 00065 mMousePos->setMargin(0); 00066 mMousePos->setBackgroundColor(TQt::red); 00067 mMousePos->setFixedSize(width(), 1); 00068 addChild(mMousePos, 0, 0); 00069 } 00070 00071 void TimeLabels::mousePosChanged(const TQPoint &pos) 00072 { 00073 moveChild(mMousePos, 0, pos.y()); 00074 } 00075 00076 void TimeLabels::showMousePos() 00077 { 00078 mMousePos->show(); 00079 } 00080 00081 void TimeLabels::hideMousePos() 00082 { 00083 mMousePos->hide(); 00084 } 00085 00086 void TimeLabels::setCellHeight(double height) 00087 { 00088 mCellHeight = height; 00089 } 00090 00091 /* 00092 Optimization so that only the "dirty" portion of the scroll view 00093 is redrawn. Unfortunately, this is not called by default paintEvent() method. 00094 */ 00095 void TimeLabels::drawContents(TQPainter *p,int cx, int cy, int cw, int ch) 00096 { 00097 // bug: the parameters cx and cw are the areas that need to be 00098 // redrawn, not the area of the widget. unfortunately, this 00099 // code assumes the latter... 00100 00101 // now, for a workaround... 00102 cx = contentsX() + frameWidth()*2; 00103 cw = contentsWidth() ; 00104 // end of workaround 00105 00106 int cell = ((int)(cy/mCellHeight)); 00107 double y = cell * mCellHeight; 00108 TQFontMetrics fm = fontMetrics(); 00109 TQString hour; 00110 TQString suffix = "am"; 00111 int timeHeight = fm.ascent(); 00112 TQFont nFont = font(); 00113 p->setFont( font() ); 00114 00115 if (!KGlobal::locale()->use12Clock()) { 00116 suffix = "00"; 00117 } else 00118 if (cell > 11) suffix = "pm"; 00119 00120 if ( timeHeight > mCellHeight ) { 00121 timeHeight = int(mCellHeight-1); 00122 int pointS = nFont.pointSize(); 00123 while ( pointS > 4 ) { 00124 nFont.setPointSize( pointS ); 00125 fm = TQFontMetrics( nFont ); 00126 if ( fm.ascent() < mCellHeight ) 00127 break; 00128 -- pointS; 00129 } 00130 fm = TQFontMetrics( nFont ); 00131 timeHeight = fm.ascent(); 00132 } 00133 //timeHeight -= (timeHeight/4-2); 00134 TQFont sFont = nFont; 00135 sFont.setPointSize( sFont.pointSize()/2 ); 00136 TQFontMetrics fmS( sFont ); 00137 int startW = mMiniWidth - frameWidth()-2 ; 00138 int tw2 = fmS.width(suffix); 00139 int divTimeHeight = (timeHeight-1) /2 - 1; 00140 //testline 00141 //p->drawLine(0,0,0,contentsHeight()); 00142 while (y < cy + ch+mCellHeight) { 00143 // hour, full line 00144 p->drawLine( cx, int(y), cw+2, int(y) ); 00145 hour.setNum(cell); 00146 // handle 24h and am/pm time formats 00147 if (KGlobal::locale()->use12Clock()) { 00148 if (cell == 12) suffix = "pm"; 00149 if (cell == 0) hour.setNum(12); 00150 if (cell > 12) hour.setNum(cell - 12); 00151 } 00152 00153 // center and draw the time label 00154 int timeWidth = fm.width(hour); 00155 int offset = startW - timeWidth - tw2 -1 ; 00156 p->setFont( nFont ); 00157 p->drawText( offset, int(y+timeHeight), hour); 00158 p->setFont( sFont ); 00159 offset = startW - tw2; 00160 p->drawText( offset, int(y+timeHeight-divTimeHeight), suffix); 00161 00162 // increment indices 00163 y += mCellHeight; 00164 cell++; 00165 } 00166 00167 } 00168 00172 int TimeLabels::minimumWidth() const 00173 { 00174 return mMiniWidth; 00175 } 00176 00178 void TimeLabels::updateConfig() 00179 { 00180 // Avoid crash on exit 00181 if ( !mAgenda ) { 00182 return; 00183 } 00184 00185 setFont(KOPrefs::instance()->mTimeBarFont); 00186 00187 TQString test = "20"; 00188 if ( KGlobal::locale()->use12Clock() ) 00189 test = "12"; 00190 mMiniWidth = fontMetrics().width( test ); 00191 if ( KGlobal::locale()->use12Clock() ) 00192 test = "pm"; 00193 else { 00194 test = "00"; 00195 } 00196 TQFont sFont = font(); 00197 sFont.setPointSize( sFont.pointSize()/2 ); 00198 TQFontMetrics fmS( sFont ); 00199 mMiniWidth += fmS.width( test ) + frameWidth()*2+4 ; 00200 // update geometry restrictions based on new settings 00201 setFixedWidth( mMiniWidth ); 00202 00203 // update HourSize 00204 mCellHeight = KOPrefs::instance()->mHourSize*4; 00205 // If the agenda is zoomed out so that more then 24 would be shown, 00206 // the agenda only shows 24 hours, so we need to take the cell height 00207 // from the agenda, which is larger than the configured one! 00208 if ( mAgenda && mCellHeight < 4*mAgenda->gridSpacingY() ) { 00209 mCellHeight = 4*mAgenda->gridSpacingY(); 00210 } 00211 resizeContents( mMiniWidth, int(mRows * mCellHeight+1) ); 00212 } 00213 00215 void TimeLabels::positionChanged() 00216 { 00217 if ( mAgenda ) { 00218 int adjustment = mAgenda->contentsY(); 00219 setContentsPos( 0, adjustment ); 00220 } 00221 } 00222 00223 void TimeLabels::positionChanged( int pos ) 00224 { 00225 setContentsPos( 0, pos ); 00226 } 00227 00229 void TimeLabels::setAgenda( KOAgenda* agenda ) 00230 { 00231 mAgenda = agenda; 00232 00233 connect(mAgenda, TQT_SIGNAL(mousePosSignal(const TQPoint &)), this, TQT_SLOT(mousePosChanged(const TQPoint &))); 00234 connect(mAgenda, TQT_SIGNAL(enterAgenda()), this, TQT_SLOT(showMousePos())); 00235 connect(mAgenda, TQT_SIGNAL(leaveAgenda()), this, TQT_SLOT(hideMousePos())); 00236 connect(mAgenda, TQT_SIGNAL(gridSpacingYChanged( double ) ), this, TQT_SLOT( setCellHeight( double ) ) ); 00237 } 00238 00239 00241 void TimeLabels::paintEvent(TQPaintEvent*) 00242 { 00243 // kdDebug(5850) << "paintevent..." << endl; 00244 // this is another hack! 00245 // TQPainter painter(this); 00246 //TQString c 00247 repaintContents(contentsX(), contentsY(), visibleWidth(), visibleHeight()); 00248 } 00249 00250 #include "timelabels.moc"