Esempio n. 1
1
QGraphicsItem *addSmiley() {
    QPen thickPen(Qt::black);
    thickPen.setWidth(2);

    // add face
    QGraphicsEllipseItem *face = new QGraphicsEllipseItem(QRect(-50, -50, 100, 100));
    face->setPen(Qt::NoPen);
    face->setBrush(Qt::yellow);

    // add eyes
    QGraphicsEllipseItem *leftEye = new QGraphicsEllipseItem(QRectF(-6, -12, 12, 24), face);
    leftEye->setPen(thickPen);
    leftEye->setBrush(Qt::white);
    leftEye->setPos(-15, -25);

    QGraphicsEllipseItem *rightEye = new QGraphicsEllipseItem(QRectF(-6, -12, 12, 24), face);
    rightEye->setPen(thickPen);
    rightEye->setBrush(Qt::white);
    rightEye->setPos(15, -25);

    // add smile

    QPainterPath smileArc;
    QRect rect(-33, -15, 66, 50);
    smileArc.arcMoveTo(rect, 0);
    smileArc.arcTo(rect, 0, -180);
    QGraphicsPathItem *smile = new QGraphicsPathItem(smileArc, face);
    smile->setPen(thickPen);

    return face;
}
void TaskItemDelegate::paintPriorityHeader(QPainter* painter, const QRectF& rect, const QModelIndex &index) const
{
  painter->save();
  
  //Draw colored gradient as priority background
  QLinearGradient gradient(rect.topLeft(), rect.bottomRight());
  gradient.setColorAt(0, itemPriorityColor(index));
  gradient.setColorAt(1, Qt::transparent);
  
  int h = QApplication::fontMetrics().height();
  QPainterPath path;
  path.moveTo(rect.bottomLeft());
  path.quadTo(rect.bottomLeft()+QPointF(0, -h), rect.bottomLeft()+QPointF(2, -h));
  path.lineTo(rect.bottomRight()-QPointF(2, h));
  path.quadTo(rect.bottomRight()+QPointF(0, -h), rect.bottomRight());
  painter->setBrush(Qt::NoBrush);
  QPen thickPen(itemPriorityColor(index));
  thickPen.setWidth(2);
  painter->setPen(thickPen);
  painter->drawPath(path);
  
  // Draw priority text
  QString priority = index.data(Qt::DisplayRole).toString();
  painter->setPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor));
  painter->setBrush(Qt::NoBrush);
  painter->drawText(rect.bottomLeft()+QPoint(MARGIN, -2*MARGIN) ,priority);
 
  painter->restore();
}
Esempio n. 3
0
void GraphicTimeLine::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/) {

	// draw a non-opaque white rectangle for the background
	painter->setPen(Qt::NoPen);
	painter->setBrush(QColor(255, 255, 255, 200));
	painter->drawRect(boundingRect());

	QFont normalFont("Liberation Sans",9,QFont::Normal);
	QPen thickPen(Qt::black, 2);
	QPen thinPen(Qt::black, 1);
	AstroDate currentDate = itsScene->getStartDay();
	AstroDate endDate = itsScene->getEndDay();

	painter->setPen(thickPen);
	painter->setFont(normalFont);

	zoom_level zl = itsScene->getZoomLevel();

	if (zl == ZM_MONTH) {
		unsigned int month;
		int xMonthPos, xWeekPos;
		//draw the first week tick
		QString weekStr = QString::number(currentDate.getWeek());
		painter->drawLine(0, 15, 0, 17);
		painter->drawText(-35, 27, "Week: ");
		painter->drawText(16 - (weekStr.length()-1)*5, 27, weekStr);

		// draw the base time line
		painter->setPen(thinPen);
		painter->drawLine(0, 15, itsWidth, 15);

		unsigned int lastMonth(currentDate.getMonth());
		currentDate = currentDate.addDays(7);
		QString monthStr;
		while (currentDate <= endDate) {
			month = currentDate.getMonth();
			if (month != lastMonth) { // draw month inset
				xMonthPos = itsScene->time2xPos(AstroDate(1,month,currentDate.getYear())) - itsScenePos;
				painter->setPen(thickPen);
				painter->drawLine(xMonthPos, 5, xMonthPos, 17);
				painter->setPen(thinPen);
				monthStr = QString(MONTH_NAMES_ENG[month-1]) + " (" + QString::number(currentDate.getYear()) + ")";
				painter->drawText(QPoint(xMonthPos + 50 - monthStr.length()/2, 11), monthStr);
				lastMonth = month;
			}
			xWeekPos = itsScene->time2xPos(currentDate) - itsScenePos;
			painter->drawLine(xWeekPos, 15, xWeekPos, 17); // week ticks
			weekStr = QString::number(currentDate.getWeek());
			painter->drawText(QPoint(xWeekPos + 16 - (weekStr.length()-1)*5, 27), weekStr);
			currentDate = currentDate.addDays(7);
		}
		xWeekPos = itsScene->time2xPos(currentDate) - itsScenePos;
		painter->drawLine(xWeekPos, 15, xWeekPos, 17); // last week thick
	}
	else if (zl == ZM_WEEK) {
		unsigned int week;
		int xDayPos, xWeekPos;
		//draw the first day tick
		QString dayStr = QString::number(currentDate.getDay());
		QString weekStr;
		painter->drawLine(0, 15, 0, 17);
		painter->drawText(-35, 27, "Day:");
		painter->drawText(20 - (dayStr.length()-1)*5, 27, dayStr);

		// draw the base time line
		painter->setPen(thinPen);
		painter->drawLine(0, 15, itsWidth, 15);

		unsigned int lastWeek(currentDate.getWeek());
		currentDate = currentDate.addDays(1);
		while (currentDate <= endDate) {
			week = currentDate.getWeek();
			if (week != lastWeek) { // draw week inset
				xWeekPos = itsScene->time2xPos(currentDate) - itsScenePos;
				painter->setPen(thickPen);
				painter->drawLine(xWeekPos, 5, xWeekPos, 17);
				painter->setPen(thinPen);
				weekStr = "week " + QString::number(currentDate.getWeek()) + " ("
					+ MONTH_SHORT_NAMES_ENG[currentDate.getMonth()-1] + " " + QString::number(currentDate.getYear()) + ")";
				painter->drawText(QPoint(xWeekPos + 120 /*- weekStr.length()*4*/, 11), weekStr);
				lastWeek = week;
			}
			xDayPos = itsScene->time2xPos(currentDate) - itsScenePos;
			painter->drawLine(xDayPos, 15, xDayPos, 17); // day ticks
			dayStr = QString::number(currentDate.getDay());
			painter->drawText(QPoint(xDayPos + 20 - (dayStr.length()-1)*5, 27), dayStr);
			currentDate = currentDate.addDays(1);
		}
		xDayPos = itsScene->time2xPos(currentDate) - itsScenePos;
		painter->drawLine(xDayPos, 15, xDayPos, 17); // last day thick
	}
	else if (zl == ZM_DAY) {
        painter->drawText(-65, 27, "Hour (UTC)");
		// draw the base time line
		painter->setPen(thinPen);
		painter->drawLine(0, 15, itsWidth, 15);
		QString dayStr;
		int xDayPos;
		while (currentDate <= endDate) {
			xDayPos = itsScene->time2xPos(currentDate) - itsScenePos;
			dayStr = QString(DAY_SHORT_NAMES_ENG[currentDate.getDayOfTheWeek()]) + " "
				+ QString::number(currentDate.getDay()) + " "
				+ MONTH_SHORT_NAMES_ENG[currentDate.getMonth()-1] + ", "
				+ QString::number(currentDate.getYear());
			painter->drawText(xDayPos + 15, 13, dayStr);
			painter->drawLine(xDayPos, 5, xDayPos, 19);
			for (unsigned int hour = 0; hour < 24; ++hour) {
				painter->drawLine(xDayPos + hour * 5, 15, xDayPos + hour * 5, 17);
			}
			painter->drawLine(xDayPos + 60, 15, xDayPos + 60, 19); // 12:00 hour longer tick
			currentDate = currentDate.addDays(1);
		}
		xDayPos = itsScene->time2xPos(currentDate) - itsScenePos;
		painter->drawLine(xDayPos, 5, xDayPos, 19); // last day thick
	}
	else if (zl == ZM_HOUR) {
        painter->drawText(-65, 27, "Hour (UTC)");
		// draw the base time line
		painter->setPen(thinPen);
		QString dayStr;
		int xDayPos;
		while (currentDate <= endDate) {
			xDayPos = itsScene->time2xPos(currentDate) - itsScenePos;
			dayStr = QString(DAY_NAMES_ENG[currentDate.getDayOfTheWeek()]) + " "
					+ QString::number(currentDate.getDay()) + " "
					+ MONTH_NAMES_ENG[currentDate.getMonth()-1] + ", "
					+ QString::number(currentDate.getYear());
			painter->drawText(xDayPos + 180, 13, dayStr);
            painter->drawImage(xDayPos -1, 3, *itsTimeLineDayImage1);
			currentDate = currentDate.addDays(1);
		}
		xDayPos = itsScene->time2xPos(currentDate) - itsScenePos;
	}
	else if (zl == ZM_MIN) {
        painter->drawText(-65, 27, "Hour (UTC)");
		// draw the base time line
        QString dayStr;
		int xDayPos;
		painter->setPen(thinPen);
		while (currentDate <= endDate) {
			xDayPos = itsScene->time2xPos(currentDate) - itsScenePos;
			dayStr = QString(DAY_NAMES_ENG[currentDate.getDayOfTheWeek()]) + " "
					+ QString::number(currentDate.getDay()) + " "
					+ MONTH_NAMES_ENG[currentDate.getMonth()-1] + ", "
					+ QString::number(currentDate.getYear());
			painter->drawText(xDayPos + 180, 13, dayStr);
            painter->drawImage(xDayPos -7, 0, *itsTimeLineDayImage2);
			currentDate = currentDate.addDays(1);
		}
		xDayPos = itsScene->time2xPos(currentDate) - itsScenePos;
	}

//	painter->setPen(QPen(Qt::magenta, 0.5));
//	painter->drawRect(boundingRect());
	// for test purpose: draw boundings
//	painter->setPen(QPen(Qt::magenta, 1.0));
//	painter->setBrush(Qt::NoBrush);
//	painter->drawRect(boundingRect());

}
void Histgram::draw(QPainter *painter)
{
    QPen thickPen(palette().foreground(), 4);
    QPen thinPen(palette().foreground(), 0.5);

    // 默认0点坐标在左上角...
    QPoint zeroPoint(width() / WidthSlices,
                     (HeightSlices - 2) * height() / HeightSlices);
    int xaxisLength = XaixsLengthRatio * width();
    int yaxisLength = YaxisLengthRatio * height();
    painter->setPen(thickPen);
    // 画出Y轴
    painter->drawLine(zeroPoint.x(), zeroPoint.y(),
                      zeroPoint.x(), zeroPoint.y() - yaxisLength);
                                                // 0_0 默认0点坐标在左上角...
    // 画出X轴
    painter->drawLine(zeroPoint.x(), zeroPoint.y(),
                      zeroPoint.x() + xaxisLength, zeroPoint.y());

    // 画出Y轴刻度
    if (voterNums <= 10)     // 检查投票总人数是否为0
    {
        if (!voterNums)
        {
            return;
        }
        YaxisSlices = 1;//投票人数少于等于10的时候改为1
    }
    int yaxisScaleNums = qRound((float)voterNums / (float)YaxisSlices);    // 刻度数目
    int yaxisScaleSpace = yaxisLength / yaxisScaleNums;     // 刻度间隔
    for (int i = 1; i <= yaxisScaleNums; i++)
    {
        painter->setPen(thickPen);
        painter->drawLine(zeroPoint.x() - ScaleSize, zeroPoint.y() - i * yaxisScaleSpace,
                          zeroPoint.x(), zeroPoint.y() - i * yaxisScaleSpace);
                                            // 是“-”...原坐标0点在左上角问题...
        QString num = QString("%1").arg(i * YaxisSlices);
        painter->drawText(zeroPoint.x() - yFrontSpace, zeroPoint.y() - i * yaxisScaleSpace, num);
        // 如果纵轴虚线被设置成显示状态
        if (yAxisDashLineFlag)
        {
            painter->setPen(QPen(Qt::black, 0.1, Qt::DashLine, Qt::SquareCap));
            painter->drawLine(zeroPoint.x(), zeroPoint.y() - i * yaxisScaleSpace,
                              zeroPoint.x() + xaxisLength, zeroPoint.y() - i * yaxisScaleSpace);
        }
    }

    // 画出柱状图和X轴
    int xaxisScaleNums = columnNums;    // 刻度数目等于列数
    int xaxisScaleSpace = xaxisLength / xaxisScaleNums;     // 刻度间隔
    int rectWidth = rectRatio * xaxisScaleSpace;        // 设置柱状条宽度
    FontSize = (width() + height()) / 70;              // 设置字体大小
    xFrontSpace = (width() + height()) / 45;            // 设置字离X轴的距离
    QFont font("Arial",FontSize,QFont::Bold,true);  // 设置字体
    for (int i = 1; i <= columnNums; i++)
    {
        // 画出X轴刻度
        painter->setPen(thickPen);
        painter->drawLine(zeroPoint.x() + i * xaxisScaleSpace, zeroPoint.y(),
                          zeroPoint.x() + i * xaxisScaleSpace, zeroPoint.y() + ScaleSize);
        // 画出选项
        painter->setFont(font);
        painter->drawText(zeroPoint.x() + i * xaxisScaleSpace -(xaxisScaleSpace + FontSize * drawData->at(i - 1).getOptionName().length()) /2, // 文字的位置在每两个刻度的中间
                          zeroPoint.y() + xFrontSpace,
                          drawData->at(i - 1).getOptionName());

        // 画柱状图
        painter->setRenderHint(QPainter::Antialiasing, true);
        thinPen.setColor("black");
        painter->setPen(thickPen);
        // style determined by rectangelMetirial
        switch (rectangleStyle)
        {
        case 1:
            {
                painter->setBrush(QBrush(colorSet->at(i), Qt::SolidPattern));
                break;
            }
        case 2:
            {
                painter->setBrush(QBrush(colorSet->at(i), Qt::Dense5Pattern));
                break;
            }
        case 3:
            {
                painter->setBrush(QBrush(colorSet->at(i), Qt::CrossPattern));
                break;
            }
        default:
            {
                // 默认为纯色
                painter->setBrush(QBrush(colorSet->at(i), Qt::SolidPattern));
            }
        }

        // 如果被设置成显示正确答案
        if (disCorrectAnswerFlag)
        {
            // 如果是正确答案,设置成绿色,否则为红色
            if (drawData->at(i - 1).getOptionName() == correctAnswer)
            {
                painter->setBrush(QBrush("green", Qt::SolidPattern));
            }
            else
            {
                painter->setBrush(QBrush("red", Qt::SolidPattern));
            }
        }


        // 长方形左上角坐标
        int rectHeight = qRound(yaxisLength * ((float)drawData->at(i - 1).getVoterNum() / (float)voterNums));
        int rectX = zeroPoint.x() + i * xaxisScaleSpace - (xaxisScaleSpace + rectWidth) / 2;
        int rectY = zeroPoint.y() - rectHeight;

        painter->drawRect(rectX, rectY, rectWidth, rectHeight);
        // 画出各项投票人数及其所占比例
        painter->setFont(font);
        QString nums = QString("%1").arg(drawData->at(i - 1).getVoterNum());    // 将投票人数转换成字符串
        QString ratio = QString("%1").arg((float)drawData->at(i - 1).getVoterNum() \
                                          / (float)voterNums * 100);   // 算出比例,专程字符串
        if (ratio.size() > precision + 1)
        {
            ratio.resize(precision + 1);        // 只显示precision位小数,算上小数点故+1
        }
        ratio += "%";
        QString text = nums + "(" + ratio + ")";
        painter->drawText(rectX + (rectWidth - FontSize * (text.length() - 3)) / 2, \
                          rectY - FontSize / 2, text);      // 3 是试出来的... - -!
    }

    // 画出题目名称
    QFont titleFont("Arial",FontSize,QFont::Bold,true);
    painter->setFont(titleFont);
    painter->drawText(zeroPoint.x(), FontSize, questionName);
}