⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datebookweek.cpp

📁 Trolltech公司发布的图形界面操作系统。可在qt-embedded-2.3.7平台上编译为嵌入式图形界面操作系统。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/************************************************************************ Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.**** This file is part of the Qtopia Environment.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "datebookweek.h"#include "datebookweekheaderimpl.h"#include <qtopia/calendar.h>#include "datebookdb.h"#include <qtopia/pim/event.h>#include <qtopia/qpeapplication.h>#include <qtopia/timestring.h>#include <qdatetime.h>#include <qheader.h>#include <qlabel.h>#include <qlayout.h>#include <qpainter.h>#include <qpopupmenu.h>#include <qtimer.h>#include <qspinbox.h>#include <qstyle.h>static const int allDayHeight = 8;static const int hourMargin = 4;class WeekViewContents : public QScrollView{    Q_OBJECTpublic:    WeekViewContents( WeekView *parent = 0, const char *name = 0 );    void showEvents( QValueList<Occurrence> &ev , const QDate &startDate );    void moveToHour( int h );    void setStartOfWeek( bool bOnMonday );    void alterDay( int );signals:    void activateWeekDay( int d );    void eventsSelected( QValueList<Occurrence> & );    void selectionCleared();protected slots:    void keyPressEvent(QKeyEvent *);    void timeStringChanged();private:    void positionItem( LayoutItem *i );    LayoutItem *intersects( const LayoutItem * );    void drawContents( QPainter *p, int cx, int cy, int cw, int ch );    void drawContents( QPainter *p);    void contentsMousePressEvent( QMouseEvent * );    void contentsMouseReleaseEvent( QMouseEvent * );    void mousePressEvent( QMouseEvent * );    void mouseReleaseEvent( QMouseEvent * );    void resizeEvent( QResizeEvent * );    void updateWeekNames(bool wsom);private:    int posOfHour(int h) const;    int hourAtPos(int p) const;    QHeader *header;    QVector<LayoutManager> items;    QList<LayoutItem> dayItems;    int rowHeight;    bool showingEvent;    WeekView *wv;};WeekViewContents::WeekViewContents( WeekView *parent, const char *name )    : QScrollView( parent, name ),      showingEvent( false ), wv(parent){    items.setAutoDelete( true );    dayItems.setAutoDelete( true );    viewport()->setBackgroundMode( PaletteBase );    header = new QHeader( this );    header->addLabel( "" );    header->setMovingEnabled( false );    header->setResizeEnabled( false );    header->setClickEnabled( false, 0 );    updateWeekNames(parent->startsOnMonday());    connect( header, SIGNAL(clicked(int)), this, SIGNAL(activateWeekDay(int)) );    TimeString::connectChange( this, SLOT(timeStringChanged()) );    QFontMetrics fm( font() );    rowHeight = fm.height()+2;    resizeContents( width(), 24*rowHeight );}void WeekViewContents::updateWeekNames(bool bOnMonday){    static bool bFirst = true;    if ( bFirst ) {	if ( bOnMonday ) {	    header->addLabel( tr("M", "Monday" ) );	    header->addLabel( tr("T", "Tuesday") );	    header->addLabel( tr("W", "Wednesday" ) );	    header->addLabel( tr("T", "Thursday" ) );	    header->addLabel( tr("F", "Friday" ) );	    header->addLabel( tr("S", "Saturday" ) );	    header->addLabel( tr("S", "Sunday" ) );	} else {	    header->addLabel( tr("S", "Sunday" ) );	    header->addLabel( tr("M", "Monday") );	    header->addLabel( tr("T", "Tuesday") );	    header->addLabel( tr("W", "Wednesday" ) );	    header->addLabel( tr("T", "Thursday" ) );	    header->addLabel( tr("F", "Friday" ) );	    header->addLabel( tr("S", "Saturday" ) );	}	bFirst = false;    } else {	// we are change things...	if ( bOnMonday ) {	    header->setLabel( 1, tr("M", "Monday") );	    header->setLabel( 2, tr("T", "Tuesday") );	    header->setLabel( 3, tr("W", "Wednesday" ) );	    header->setLabel( 4, tr("T", "Thursday" ) );	    header->setLabel( 5, tr("F", "Friday" ) );	    header->setLabel( 6, tr("S", "Saturday" ) );	    header->setLabel( 7, tr("S", "Sunday" ) );	} else {	    header->setLabel( 1, tr("S", "Sunday" ) );	    header->setLabel( 2, tr("M", "Monday") );	    header->setLabel( 3, tr("T", "Tuesday") );	    header->setLabel( 4, tr("W", "Wednesday" ) );	    header->setLabel( 5, tr("T", "Thursday" ) );	    header->setLabel( 6, tr("F", "Friday" ) );	    header->setLabel( 7, tr("S", "Saturday" ) );	}    }}void WeekViewContents::showEvents( QValueList<Occurrence> &ev, const QDate &startWeek ){    items.clear();    items.resize(7);    int i;    for (i = 0; i < 7; i++) {	LayoutManager *lm = new LayoutManager(header->sectionSize(i+1), rowHeight*24);	lm->setDate(startWeek.addDays(i));	items.insert(i, lm);    }    dayItems.clear();    QValueListIterator<Occurrence> it;    for ( it = ev.begin(); it != ev.end(); ++it ) {	if ((*it).event().isAllDay()) {	    LayoutItem *i = new LayoutItem(*it);	    positionItem(i);	    dayItems.append(i);	} else {	    int firstdow = startWeek.daysTo((*it).startInCurrentTZ().date());	    int lastdow = startWeek.daysTo((*it).endInCurrentTZ().date());	    if (firstdow < 0) firstdow = 0;	    if (lastdow > 6) lastdow = 6;	    for (int i = firstdow; i <= lastdow; i++)		items.at(i)->addOccurrence( *it );	}    }    int mWidth = contentsWidth();    for (i = 0; i < 7; i++) {	items.at(i)->layoutItems(FALSE);	mWidth = QMIN(mWidth, items.at(i)->maximumColumnWidth());    }    for (i = 0; i < 7; i++) {	items.at(i)->setMaximumColumnWidth(mWidth);	items.at(i)->layoutItems(FALSE);    }    viewport()->update();    update();}void WeekViewContents::moveToHour( int h ){    int offset = h*rowHeight;    setContentsPos( 0, offset );}void WeekViewContents::keyPressEvent( QKeyEvent *e ){    e->ignore();}void WeekViewContents::timeStringChanged(){    viewport()->update();}static inline int db_round30min( int m ){    if ( m < 15 )	m = 0;    else if ( m < 45 )	m = 1;    else	m = 2;    return m;}void WeekViewContents::alterDay( int day ){    if ( !wv->startsOnMonday() ) {	    day--;    }    emit activateWeekDay( day );}// Only for all day items which we have a very custom layout for.void WeekViewContents::positionItem( LayoutItem *i ){    const Occurrence ev = i->occurrence();    int dow, duration;    if (ev.startInCurrentTZ().date() < ((WeekView *)parent())->weekDate())  {	dow = 0;	duration = ((WeekView *)parent())->weekDate().daysTo(		ev.endInCurrentTZ().date());    } else  {	dow = ev.startInCurrentTZ().date().dayOfWeek();	duration = ev.startInCurrentTZ().date().daysTo(	    ev.endInCurrentTZ().date());    }    if ( !wv->startsOnMonday() ) {	if ( dow == 7 )	    dow = 1;	else	    dow++;    }    // now make sure duration doesn't extend of the end fo the week.    if (dow + duration > 7) {	duration = 7 - dow;    }    int x = header->sectionPos( dow );    int hwidth = header->sectionSize( dow );    hwidth += hwidth*duration;    LayoutItem *inter = 0;    i->setGeometry(x, header->height(), hwidth, allDayHeight * 2);    inter = intersects( i );    if (inter) {	// need to change geom of both;	i->setGeometry( x, header->height() + 		(ev.event().hasRepeat() ? allDayHeight : 0), 		hwidth , allDayHeight);	inter->setGeometry( inter->geometry().x(), header->height() +		(inter->event().hasRepeat() ? allDayHeight : 0), 		inter->geometry().width(), allDayHeight);    }}LayoutItem *WeekViewContents::intersects( const LayoutItem *item ){    QRect geom = item->geometry();    // We allow the edges to overlap    geom.moveBy( 1, 1 );    geom.setSize( geom.size()-QSize(2,2) );    const PimEvent itemEvent = item->event();    QListIterator<LayoutItem> it(dayItems);    for ( ; it.current(); ++it ) {	LayoutItem *i = it.current();	const PimEvent iEvent = i->event();	if (i != item && iEvent.hasRepeat() != itemEvent.hasRepeat()) {	    if (i->geometry().intersects( geom ) )		return i;	}    }    return 0;}void WeekViewContents::contentsMousePressEvent( QMouseEvent *e ){    QValueList<Occurrence> list;    for (int j = 0; j < 7; j++) {	QVector<LayoutItem> v = items.at(j)->items();	int x = header->sectionPos(j+1)-1;	for ( uint k = 0; k < v.size(); k++) {	    LayoutItem *i = v.at(k);	    // Change to	    QRect geo = i->geometry();	    geo.moveBy(x,0);	    if ( geo.contains( e->pos() ) ) {		showingEvent = true;		list.append(i->occurrence());	    }	}    }    if (list.count())	emit eventsSelected( list );}void WeekViewContents::contentsMouseReleaseEvent( QMouseEvent *e ){    if ( showingEvent ) {	showingEvent = false;	emit selectionCleared();    } else {	int d = header->sectionAt( e->pos().x() );	if ( d > 0 ) {// 	    if ( !bOnMonday )// 		d--;	    emit activateWeekDay( d );	}    }}void WeekViewContents::mousePressEvent( QMouseEvent *e ){//#if (QT_VERSION-0 >= 0x030000)#if 0    // hack to get around dumb 3.1 scrollview mouse events.    if  (e->pos().y() > topMargin())	return;#endif    QValueList<Occurrence> list;    QListIterator<LayoutItem> it(dayItems);    for ( ; it.current(); ++it ) {	LayoutItem *i = it.current();	if ( i->geometry().contains( e->pos() ) ) {	    showingEvent = true;	    list.append(i->occurrence());	}    }    if (list.count())	emit eventsSelected( list );}void WeekViewContents::mouseReleaseEvent( QMouseEvent *e ){//#if (QT_VERSION-0 >= 0x030000)#if 0    // hack to get around dumb 3.1 scrollview mouse events.    if  (e->pos().y() > topMargin())	return;#endif    if ( showingEvent ) {	showingEvent = false;	emit selectionCleared();    } else {	int d = header->sectionAt( e->pos().x() );	if ( d > 0 ) {	    emit activateWeekDay( d );	}    }}void WeekViewContents::drawContents( QPainter *p ) {    p->fillRect(0, header->height(), header->width(), allDayHeight * 2, 	    white);	    //palette().color(QPalette::Normal , QColorGroup::Dark ));    // draw day events.      QListIterator<LayoutItem> it(dayItems);    for ( ; it.current(); ++it ) {	LayoutItem *i = it.current();	if ( i->event().isAllDay() ) {	    p->fillRect( i->geometry(), i->event().color().light(175));	}    }    QPen pen = p->pen();    p->setPen( lightGray );    // header->sectionPos(i)-1 because we want the end of the last header,    // not the start of this one.    for ( int i = 1; i <= 7; i++ )	p->drawLine( header->sectionPos(i)-1, header->height(), 		header->sectionPos(i)-1, header->height() + 2 * allDayHeight );    p->drawLine( 0, header->height() + 2 * allDayHeight, 	    header->width(), header->height() + 2 * allDayHeight );    p->setPen(pen);}void WeekViewContents::drawContents( QPainter *p, int cx, int cy, int cw, int ch ){    QRect ur( cx, cy, cw, ch );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -