korganizer

timespanwidget.cpp
00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
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 <tqsplitter.h>
00026 #include <tqlistview.h>
00027 #include <tqlayout.h>
00028 #include <tqheader.h>
00029 #include <tqpushbutton.h>
00030 
00031 #include <klocale.h>
00032 #include <kdebug.h>
00033 
00034 #include <libkcal/event.h>
00035 
00036 #include "lineview.h"
00037 #include "timeline.h"
00038 
00039 #include "timespanwidget.h"
00040 #include "timespanwidget.moc"
00041 
00042 TimeSpanWidget::TimeSpanWidget( TQWidget *parent, const char *name ) :
00043   TQWidget( parent, name )
00044 {
00045   TQBoxLayout *topLayout = new TQVBoxLayout( this );
00046 
00047   mSplitter = new TQSplitter( this );
00048   topLayout->addWidget( mSplitter );
00049 
00050   mList = new TQListView( mSplitter );
00051   mList->addColumn( i18n("Summary") );
00052   
00053   TQWidget *rightPane = new TQWidget( mSplitter );
00054   TQBoxLayout *rightPaneLayout = new TQVBoxLayout( rightPane );
00055 
00056   mTimeLine = new TimeLine( rightPane );
00057   mTimeLine->setFixedHeight( mList->header()->height() );
00058   rightPaneLayout->addWidget( mTimeLine );
00059   
00060   mLineView = new LineView( rightPane );
00061   rightPaneLayout->addWidget( mLineView );
00062 
00063   TQBoxLayout *buttonLayout = new TQHBoxLayout( rightPaneLayout );
00064   
00065   TQPushButton *zoomInButton = new TQPushButton( i18n("Zoom In"), rightPane );
00066   connect( zoomInButton, TQT_SIGNAL( clicked() ), TQT_SLOT( zoomIn() ) );
00067   buttonLayout->addWidget( zoomInButton );
00068   
00069   TQPushButton *zoomOutButton = new TQPushButton( i18n("Zoom Out"), rightPane );
00070   connect( zoomOutButton, TQT_SIGNAL( clicked() ), TQT_SLOT( zoomOut() ) );
00071   buttonLayout->addWidget( zoomOutButton );
00072   
00073   TQPushButton *centerButton = new TQPushButton( i18n("Center View"), rightPane );
00074   connect( centerButton, TQT_SIGNAL( clicked() ), TQT_SLOT( centerView() ) );
00075   buttonLayout->addWidget( centerButton );
00076 
00077   connect(mLineView->horizontalScrollBar(),TQT_SIGNAL(valueChanged(int)),
00078           mTimeLine,TQT_SLOT(setContentsPos(int)));
00079 }
00080 
00081 TimeSpanWidget::~TimeSpanWidget()
00082 {
00083 }
00084 
00085 TQValueList<int> TimeSpanWidget::splitterSizes()
00086 {
00087   return mSplitter->sizes();
00088 }
00089 
00090 void TimeSpanWidget::setSplitterSizes( TQValueList<int> sizes )
00091 {
00092   mSplitter->setSizes( sizes );
00093 }
00094 
00095 void TimeSpanWidget::addItem( KCal::Event *event )
00096 {
00097   new TQListViewItem( mList, event->summary() );
00098   
00099   TQDateTime startDt = event->dtStart();
00100   TQDateTime endDt = event->dtEnd();
00101 
00102 //  kdDebug(5850) << "TimeSpanWidget::addItem(): start: " << startDt.toString()
00103 //            << "  end: " << endDt.toString() << endl;
00104 
00105 //  int startSecs = mStartDate.secsTo( startDt );
00106 //  int durationSecs = startDt.secsTo( endDt );
00107   
00108 //  kdDebug(5850) << "--- startSecs: " << startSecs << "  dur: " << durationSecs << endl;
00109 
00110   int startX = mStartDate.secsTo( startDt ) / mSecsPerPixel;
00111   int endX = startX + startDt.secsTo( endDt ) / mSecsPerPixel;
00112   
00113 //  kdDebug(5850) << "TimeSpanWidget::addItem(): s: " << startX << "  e: " << endX << endl;
00114   
00115   mLineView->addLine( startX, endX );
00116 }
00117 
00118 void TimeSpanWidget::clear()
00119 {
00120   mList->clear();
00121   mLineView->clear();
00122 }
00123 
00124 void TimeSpanWidget::updateView()
00125 {
00126   mLineView->updateContents();
00127   mTimeLine->updateContents();
00128 }
00129 
00130 void TimeSpanWidget::setDateRange( const TQDateTime &start, const TQDateTime &end )
00131 {
00132   mStartDate = start;
00133   mEndDate = end;
00134   
00135   mTimeLine->setDateRange( start, end );
00136 
00137   mSecsPerPixel = mStartDate.secsTo( mEndDate ) / mLineView->pixelWidth();
00138 }
00139 
00140 TQDateTime TimeSpanWidget::startDateTime()
00141 {
00142   return mStartDate;
00143 }
00144 
00145 TQDateTime TimeSpanWidget::endDateTime()
00146 {
00147   return mEndDate;
00148 }
00149 
00150 void TimeSpanWidget::zoomIn()
00151 {
00152   int span = mStartDate.daysTo( mEndDate );
00153   setDateRange( mStartDate.addDays( span / 4 ), mEndDate.addDays( span / -4 ) );
00154 
00155   emit dateRangeChanged();
00156 }
00157 
00158 void TimeSpanWidget::zoomOut()
00159 {
00160   int span = mStartDate.daysTo( mEndDate );
00161   setDateRange( mStartDate.addDays( span / -4 ), mEndDate.addDays( span / 4 ) );
00162 
00163   emit dateRangeChanged();
00164 }
00165 
00166 void TimeSpanWidget::centerView()
00167 {
00168   TQScrollBar *scrollBar = mLineView->horizontalScrollBar();
00169   int min = scrollBar->minValue();
00170   int max = scrollBar->maxValue();
00171   scrollBar->setValue( min + (max-min) / 2 );
00172 }