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

📄 dayview.cpp

📁 Trolltech公司发布的图形界面操作系统。可在qt-embedded-2.3.7平台上编译为嵌入式图形界面操作系统。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    setMetrics( QFontMetrics(qApp->font() ));}void DayView::fontChange( const QFont & ){    view->setMetrics( QFontMetrics(qApp->font() ));    allView->setMetrics( QFontMetrics(qApp->font() ));    getEvents();}void DayViewContents::setMetrics( const QFontMetrics &fm){    time_height = QMIN(fm.height() * 3, QMAX(fm.height(), height() / 10));    day_height = fm.height() + 5;#ifdef QTOPIA_DESKTOP    day_height += 2;	// difference in font metrics?#endif    // resize children to fit if that is our type.    switch(typ) {	case ScrollingDay:	    resizeContents(		    width() - style().scrollBarExtent().width(),		    posOfHour(24));	    setContentsPos(0, posOfHour(start_of_day));	    break;	case CompressedDay:	    break;	case AllDay:	    resizeContents(		    itemList->count() > 4		    ? width() - style().scrollBarExtent().width()		    : width(),		    day_height * ((itemList->count() + 1) / 2 ));	    break;    }    itemList->setSize(	    contentsWidth() - (typ == AllDay ? 0 : time_width) - 1,	    contentsHeight());    itemList->layoutItems();    updateContents(0,0, contentsWidth(), contentsHeight());}void DayViewContents::contentsMousePressEvent( QMouseEvent *e ){    SortedLayoutItems list = itemList->items();    dragging = TRUE;    for (uint i = 0; i < list.size(); i++) {	LayoutItem *itm = list.at(i);	QRect geo = itm->geometry();	if (typ != AllDay)	    geo.moveBy(time_width, 0);	if (geo.contains( e->pos() ) ) {	    Occurrence oc = itm->occurrence();	    PimEvent ev = itm->event();	    QPopupMenu m;	    m.insertItem( tr( "Edit" ), 1 );	    m.insertItem( tr( "Delete" ), 2 );#ifdef Q_WS_QWS	    if (Ir::supported()) {		m.insertItem( tr( "Beam" ), 3 );	    }#endif	    int r = m.exec( e->globalPos() );	    if ( r == 1 ) {		emit editOccurrence( oc );	    } else if ( r == 2 ) {		emit removeOccurrence( oc );	    } else if ( r == 3 ) {		emit beamEvent( ev );	    }	    dragging = FALSE;	    break;	}    }    if (dragging) {	int h = hourAtPos(e->pos().y());	int oldSelMin = QMIN(startSel, endSel);	int oldSelMax = QMAX(startSel, endSel);	startSel = endSel = h;	oldSelMin = oldSelMin == -1 ? startSel : QMIN(oldSelMin, startSel);	oldSelMax = oldSelMax == -1 ? startSel : QMAX(oldSelMax, startSel);	repaintContents(0, posOfHour(oldSelMin - 1),		time_width, posOfHour(oldSelMax + 1), FALSE);	dragging = TRUE;    }}void DayViewContents::contentsMouseMoveEvent( QMouseEvent *e ){    if (dragging) {	int h = hourAtPos(e->pos().y());	if (h == endSel)	    return;	int oldSelMin = QMIN(startSel, endSel);	int oldSelMax = QMAX(startSel, endSel);	endSel = h;	oldSelMin = QMIN(oldSelMin, endSel);	oldSelMax = QMAX(oldSelMax, endSel);	repaintContents(0, posOfHour(oldSelMin - 1),		time_width, posOfHour(oldSelMax + 1), FALSE);    }}void DayViewContents::contentsMouseReleaseEvent( QMouseEvent *e ){    if (dragging) {	int h = hourAtPos(e->pos().y());	int oldSelMin = QMIN(startSel, endSel);	int oldSelMax = QMAX(startSel, endSel);	endSel = h;	oldSelMin = QMIN(oldSelMin, endSel);	oldSelMax = QMAX(oldSelMax, endSel);	repaintContents(0, posOfHour(oldSelMin - 1),		time_width, posOfHour(oldSelMax + 1), FALSE);	// XXX should signal new event.	emit keyPressed(QString::null);    }}void DayViewContents::keyPressEvent( QKeyEvent *e ){    QString txt = e->text();    if ( !txt.isNull() && txt[0] > ' ' && e->key() < 0x1000 ) {	// we this is some sort of thing we know about...	e->accept();	emit keyPressed( txt );    } else {	// I don't know what this key is, do you?	e->ignore();    }}void DayViewContents::drawContents( QPainter *p, int, int y, int, int h){    bool isClipped = p->hasClipping();    QRegion oldClip = p->clipRegion();    QRegion bgr = isClipped ? oldClip : QRect(0, 0, contentsWidth(), contentsHeight() );    QRect clipRect( 0, y, contentsWidth(), h );    bgr &= clipRect;    // draw items first and clip away their geometries to reduce flicker.    for (int k = 0; k < itemList->count(); k++) {	DayItem *di = (DayItem*)itemList->items().at(k);	QRect r = di->geometry();	r = expandRect( r );	r.moveBy( typ == AllDay ? 0 : time_width, 0 );	if ( r.intersects( clipRect ) ) {	    di->drawItem(p, typ == AllDay ? 0 : time_width, 0, palette().active());	    bgr -= r;	}    }    bgr.translate( -contentsX(), -contentsY() );    p->setClipRegion( bgr );    //p->fillRect(time_width, x+w-time_width, y, h, palette().active().color(QColorGroup::Base));    p->fillRect(time_width, 0,	    contentsWidth() - time_width, contentsHeight(), white);    int firstSel = QMIN(startSel, endSel);    int lastSel = QMAX(startSel, endSel);    // draw each hour.    QFont f = p->font();    QFontMetrics fm(f);    int base = QMIN(time_height - fm.height(), 2);    base = time_height - (base / 2) - (fm.height() - fm.ascent());    base -= 2;    QColorGroup cgUp = palette().active();    cgUp.setColor(QColorGroup::Button,	    //cgUp.color(QColorGroup::Button).light(100));	    palette().active().color(QColorGroup::Midlight));    QFont selectFont = f;    selectFont.setBold(TRUE);    // also of possible use, QColorGroup::BrightText.    switch (typ) {	case ScrollingDay:	    {		int rx = 0;		int ry = 0;		int rh = 0;		int t = 0;		while (t < 24) {		    QString s = TimeString::localH(t);		    s += " ";		    ry = posOfHour(t);		    t++;		    rh = posOfHour(t) - ry;		    if (ry > y + h)			break;		    if (ry + rh < y)			continue;		    // draw this rect.		    QPen pn = p->pen();		    //p->setPen(black);		    p->drawLine( time_width, ry - 1, width(), ry - 1);		    bool isDown;		    if ( t > firstSel && t <= lastSel + 1 ){			p->setFont(selectFont);			isDown = TRUE;		    } else {			isDown = FALSE;		    }#if (QT_VERSION-0 >= 0x030000)		    style().drawPrimitive(			    QStyle::PE_ButtonBevel,			    p,			    QRect(rx, ry, time_width, time_height),			    cgUp, isDown ? QStyle::Style_Enabled | QStyle::Style_Down : QStyle::Style_Raised);//QStyle::Style_Enabled | QStyle::Style_Up);		    //style().drawToolButton(p, rx, ry, time_width, time_height,			    //cgUp, t > firstSel && t <= lastSel + 1 );		    style().drawItem(p, QRect(rx, ry, time_width, time_height),			    AlignRight | AlignTop, cgUp, TRUE, 0, s);#else		    p->fillRect( rx, ry, time_width, time_height, white );		    style().drawToolButton(p, rx, ry, time_width, time_height,			    cgUp, t > firstSel && t <= lastSel + 1 );		    style().drawItem(p, rx, ry, time_width, time_height,			    AlignRight | AlignTop, cgUp, TRUE, 0, s);#endif		    p->setFont(f);		    p->setPen(pn);		}	    }	    break;	case CompressedDay:	    break;	case AllDay:	    break;    }    if ( isClipped )	p->setClipRegion( oldClip );    p->setClipping( isClipped );}int DayViewContents::posOfHour(int h) const {    switch (typ) {	case ScrollingDay:	    // all hours, regardless of height.	    return h < 0 ? 0 : h*time_height;	case CompressedDay:	    // all hours, in height.	    break;	default:	    return 0;    }    return 0;}int DayViewContents::hourAtPos(int h) const {    switch (typ) {	case ScrollingDay:	    // all hours, regardless of height.	    return h == 0 ? 0 : h/time_height;	case CompressedDay:	    // all hours, in height.	    break;	default:	    return 0;    }    return 0;}//===========================================================================DayView::DayView( DateBookTable *newDb, bool startOnMonday,			  QWidget *parent, const char *name )    : PeriodView( newDb, startOnMonday, parent, name ){    QFontMetrics fm( font() );    day_height = fm.height() + 5;#ifdef QTOPIA_DESKTOP    day_height += 2;	// difference in font metrics?#endif    QVBoxLayout *layout = new QVBoxLayout(this);#ifdef QTOPIA_NO_POINTER_INPUT    header = new QLabel(this, "day header");    header->setBackgroundMode(PaletteButton);    header->setAlignment(AlignHCenter);    header->setText(headerText(cDate));#else    header = new DayViewHeader( startOnMonday, this, "day header" ); // No tr    header->setDate( cDate.year(), cDate.month(), cDate.day() );#endif    layout->addWidget(header);    allView = new DayViewContents( DayViewContents::AllDay,	    this, "all day view" ); // No tr    allView->setFocusPolicy(NoFocus);    layout->addWidget(allView);    view = new DayViewContents( DayViewContents::ScrollingDay,	    this, "day view" ); // No tr    view->setSizePolicy(	    QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));    view->setFrameStyle( QFrame::NoFrame );    allView->setFrameStyle( QFrame::NoFrame );    layout->addWidget(view);#ifndef QTOPIA_NO_POINTER_INPUT    connect( header, SIGNAL( dateChanged( const QDate & ) ),             this, SLOT( selectDate( const QDate &) ) );#endif    connect( view, SIGNAL( sigColWidthChanged() ),             this, SLOT( slotColWidthChanged() ) );    connect( view, SIGNAL(keyPressed(const QString &)),	     this, SIGNAL(newEvent(const QString&)) );    connect( view, SIGNAL(editEvent(const PimEvent &)),	     this, SIGNAL(editEvent(const PimEvent&)) );    connect( view, SIGNAL(removeEvent(const PimEvent &)),	     this, SIGNAL(removeEvent(const PimEvent&)) );    connect( view, SIGNAL(beamEvent(const PimEvent &)),	     this, SIGNAL(beamEvent(const PimEvent&)) );    connect( view, SIGNAL(editOccurrence(const Occurrence &)),	     this, SIGNAL(editOccurrence(const Occurrence&)) );    connect( view, SIGNAL(removeOccurrence(const Occurrence &)),	     this, SIGNAL(removeOccurrence(const Occurrence&)) );    connect( allView, SIGNAL(editOccurrence(const Occurrence &)),	     this, SIGNAL(editOccurrence(const Occurrence&)) );    connect( allView, SIGNAL(removeOccurrence(const Occurrence &)),	     this, SIGNAL(removeOccurrence(const Occurrence&)) );    connect( allView, SIGNAL(beamEvent(const PimEvent &)),	     this, SIGNAL(beamEvent(const PimEvent&)) );    setFocusPolicy(StrongFocus);    setFocus();}#ifdef QTOPIA_NO_POINTER_INPUTQString DayView::headerText(const QDate &d) const{    return tr("%1 - %2", "Date as day of week, followed by date in local format")	.arg(TimeString::localDayOfWeek(d, TimeString::Long)).arg(TimeString::localYMD(d));}#endifvoid DayView::selectedDates( QDateTime &start, QDateTime &end ){    start.setDate( cDate );    end.setDate( cDate );    int sh = view->startHour();    int eh = view->endHour();    if (sh >= 0) {	start.setTime( QTime( sh, 0, 0 ) );	if (eh == 24) {	    end.setDate(cDate.addDays(1));	    end.setTime( QTime(0,0,0) );	} else {	    end.setTime( QTime( eh, 0, 0 ) );	}    }}void DayView::clearSelectedDates(){    view->clearSelectedTimes();}void DayView::selectDate( const QDate &d){    if ( cDate == d )	return;    PeriodView::selectDate(d);#ifdef QTOPIA_NO_POINTER_INPUT    header->setText(headerText(d));#else    header->setDate( d.year(), d.month(), d.day() );#endif    view->selectDate(d);    relayoutPage();}bool DayView::hasSelection() const{    return (allView->currentItem() || view->currentItem());}PimEvent DayView::currentEvent() const{    DayItem *dim = allView->currentItem();    if (dim)	return dim->event();    dim = view->currentItem();    if (dim)	return dim->event();    return PimEvent();}Occurrence DayView::currentItem() const{    DayItem *dim = allView->currentItem();    if (dim)	return dim->occurrence();    dim = view->currentItem();    if (dim)	return dim->occurrence();    return Occurrence();}void DayView::setCurrentItem( const Occurrence &o ){    setCurrentEvent(o.event());}void DayView::setCurrentEvent( const PimEvent &e ){    viewWithFocus = 0;    DayItem *orig = allView->currentItem();    DayItem *dim = allView->firstItem(FALSE);    if (dim) {	// check all view;	do {	    if (dim->event() == e) {		allView->setCurrentItem( dim, FALSE );		allView->moveSelection(orig, dim);		if (!orig)		    view->setCurrentItem(0);		return;	    }	} while ((dim = allView->nextItem(FALSE)) != 0);    }    orig = view->currentItem();    // if found in all view, would have returned.    viewWithFocus = 1;    dim = view->firstItem(FALSE);    if (dim) {	// check all view;	do {	    if (dim->event() == e) {		view->setCurrentItem( dim, FALSE );		view->moveSelection(orig, dim);		if (!orig)		    allView->setCurrentItem(0);		return;	    }	} while ((dim = view->nextItem(FALSE)) != 0);    }}void DayView::redraw(){    //if ( isUpdatesEnabled() )	relayoutPage();}void DayView::getEvents(){    view->clearItems();    allView->clearItems();    QValueList<Occurrence> eventList = db->getOccurrences( cDate, cDate );    int allDayCount = 0;    QValueListIterator<Occurrence> it;    for ( it = eventList.begin(); it != eventList.end(); ++it ) {	if ((*it).event().isAllDay()) {	    allView->addOccurrence(*it, cDate);	    allDayCount++;	} else {	    view->addOccurrence(*it, cDate);	}    }    view->layoutItems();    if (allDayCount) {	int fixed_height;	if (allDayCount > 4)	    fixed_height = 2 * day_height + (day_height / 2 );	else	    fixed_height = day_height * (++allDayCount / 2 );	allView->setMaximumHeight(fixed_height);	allView->setMinimumHeight(fixed_height);	allView->layoutItems();	allView->show();	allView->firstItem();	viewWithFocus = 0;    } else {	allView->hide();	view->firstItem();	viewWithFocus = 1;    }    emit selectionChanged();}void DayView::relayoutPage( bool fromResize ){    if ( !fromResize )        getEvents();    // no need we already have them!    view->layoutItems();    allView->layoutItems();    return;}void DayView::setDayStarts( int startHere ){    PeriodView::setDayStarts(startHere);    view->startAtTime(startHere);}void DayView::setStartOnMonday( bool bStartOnMonday ){#ifndef QTOPIA_NO_POINTER_INPUT    header->setStartOfWeek( bStartOnMonday );#endif    //    redraw();}void DayView::keyPressEvent(QKeyEvent *e){    switch(e->key()) {	case Key_Up:	    if (viewWithFocus == 0) {		if (!allView->previousItem())		    allView->firstItem();	    } else  {		if (!view->previousItem()) {		    if (allView->lastItem())			viewWithFocus = 0;		    else			view->firstItem();		}	    }	    break;	case Key_Down:	    if (viewWithFocus == 0) {		if (!allView->nextItem()) {		    if (view->firstItem())			viewWithFocus = 1;		    else			allView->lastItem();		}	    } else  {		if (!view->nextItem())		    view->lastItem();	    }	    break;	case Key_Left:	    selectDate(QDate(cDate).addDays(-1));	    break;	case Key_Right:	    selectDate(QDate(cDate).addDays(1));	    break;	default:	    e->ignore();    }}

⌨️ 快捷键说明

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