00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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 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 QFrame(this);
00064 mMousePos->setLineWidth(0);
00065 mMousePos->setMargin(0);
00066 mMousePos->setBackgroundColor(Qt::red);
00067 mMousePos->setFixedSize(width(), 1);
00068 addChild(mMousePos, 0, 0);
00069 }
00070
00071 void TimeLabels::mousePosChanged(const QPoint &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
00093
00094
00095 void TimeLabels::drawContents(QPainter *p,int cx, int cy, int cw, int ch)
00096 {
00097
00098
00099
00100
00101
00102 cx = contentsX() + frameWidth()*2;
00103 cw = contentsWidth() ;
00104
00105
00106 int cell = ((int)(cy/mCellHeight));
00107 double y = cell * mCellHeight;
00108 QFontMetrics fm = fontMetrics();
00109 QString hour;
00110 QString suffix = "am";
00111 int timeHeight = fm.ascent();
00112 QFont 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 = QFontMetrics( nFont );
00126 if ( fm.ascent() < mCellHeight )
00127 break;
00128 -- pointS;
00129 }
00130 fm = QFontMetrics( nFont );
00131 timeHeight = fm.ascent();
00132 }
00133
00134 QFont sFont = nFont;
00135 sFont.setPointSize( sFont.pointSize()/2 );
00136 QFontMetrics fmS( sFont );
00137 int startW = mMiniWidth - frameWidth()-2 ;
00138 int tw2 = fmS.width(suffix);
00139 int divTimeHeight = (timeHeight-1) /2 - 1;
00140
00141
00142 while (y < cy + ch+mCellHeight) {
00143
00144 p->drawLine( cx, int(y), cw+2, int(y) );
00145 hour.setNum(cell);
00146
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
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
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 setFont(KOPrefs::instance()->mTimeBarFont);
00181
00182 QString test = "20";
00183 if ( KGlobal::locale()->use12Clock() )
00184 test = "12";
00185 mMiniWidth = fontMetrics().width( test );
00186 if ( KGlobal::locale()->use12Clock() )
00187 test = "pm";
00188 else {
00189 test = "00";
00190 }
00191 QFont sFont = font();
00192 sFont.setPointSize( sFont.pointSize()/2 );
00193 QFontMetrics fmS( sFont );
00194 mMiniWidth += fmS.width( test ) + frameWidth()*2+4 ;
00195
00196 setFixedWidth( mMiniWidth );
00197
00198
00199 mCellHeight = KOPrefs::instance()->mHourSize*4;
00200
00201
00202
00203 if ( mAgenda && mCellHeight < 4*mAgenda->gridSpacingY() ) {
00204 mCellHeight = 4*mAgenda->gridSpacingY();
00205 }
00206 resizeContents( mMiniWidth, int(mRows * mCellHeight+1) );
00207 }
00208
00210 void TimeLabels::positionChanged()
00211 {
00212 if ( mAgenda ) {
00213 int adjustment = mAgenda->contentsY();
00214 setContentsPos( 0, adjustment );
00215 }
00216 }
00217
00218 void TimeLabels::positionChanged( int pos )
00219 {
00220 setContentsPos( 0, pos );
00221 }
00222
00224 void TimeLabels::setAgenda( KOAgenda* agenda )
00225 {
00226 mAgenda = agenda;
00227
00228 if ( mAgenda ) {
00229 connect(mAgenda, SIGNAL(mousePosSignal(const QPoint &)),
00230 this, SLOT(mousePosChanged(const QPoint &)));
00231 connect(mAgenda, SIGNAL(enterAgenda()), this, SLOT(showMousePos()));
00232 connect(mAgenda, SIGNAL(leaveAgenda()), this, SLOT(hideMousePos()));
00233 connect(mAgenda, SIGNAL(gridSpacingYChanged(double)),
00234 this, SLOT(setCellHeight(double)) );
00235 }
00236 }
00237
00238
00240 void TimeLabels::paintEvent(QPaintEvent*)
00241 {
00242
00243
00244
00245
00246 repaintContents(contentsX(), contentsY(), visibleWidth(), visibleHeight());
00247 }
00248
00249 #include "timelabels.moc"