PDA

View Full Version : Adding numbers to circles in QPaint



therealjag
12th February 2006, 11:08
hey there, i created a little application that lets the user create circles and connect them with lines using the tooltips example as a base. im just wondering if theres a way to number the circles so that everytime a user creates a new circle it gets numbered starting from 0. im sure its to do wth the shapeitem.cpp file but i dont know what to add to make it work. any ideas??? ive listed my shapeItem.cpp file below and most of the sortingBox.cpp file.....any help would be much appreciated because i am really stuck with this - thanks, Jag

-----------------------------------shapeItem.cpp--------------------------------------------
#include <QtGui>

#include "shapeitem.h"

QPainterPath ShapeItem::path() const
{
return myPath;
}

QPoint ShapeItem::position() const
{
return myPosition;
}

QColor ShapeItem::color() const
{
return myColor;
}

QString ShapeItem::toolTip() const
{
return myToolTip;
}

void ShapeItem::setPath(const QPainterPath &path)
{
myPath = path;
}

void ShapeItem::setToolTip(const QString &toolTip)
{
myToolTip = toolTip;
}

void ShapeItem::setPosition(const QPoint &position)
{
myPosition = position;
}

void ShapeItem::setColor(const QColor &color)
{
myColor = color;
}

------------------------------------------------------------------------------------------
----------------------------------------sortingBox.cpp----------------------------------

#include <QtGui>
#include <stdlib.h>
#include <QtCore>

#include "sortingbox.h"

SortingBox::SortingBox()///////////////////CONSTRUCTOR////////////////////////////
{
setAttribute(Qt::WA_StaticContents);
setMouseTracking(true);
setBackgroundRole(QPalette::Base);

itemInMotion = 0;
points1 = new QPoint[MAXPOINTS];
points2 = new QPoint[MAXPOINTS];
count = 0;
down = FALSE;
recordLine = FALSE;


circlePath.addEllipse(QRect(0, 0, 20, 20));//used to change size of the circle//

setWindowTitle(tr("Topology Creator"));
resize(500, 300);


}

SortingBox::~SortingBox()///////////////////DE-CONSTRUCTOR////////////////////////////
{
delete[] points1; // cleanup
delete[] points2; // cleanup
}


bool SortingBox::event(QEvent *event)
{
if (event->type() == QEvent::ToolTip) {
QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
int index = itemAt(helpEvent->pos());
if (index != -1)
QToolTip::showText(helpEvent->globalPos(), shapeItems[index].toolTip());
}
return QWidget::event(event);
}

void SortingBox::resizeEvent(QResizeEvent * /* event */)
{
int margin = style()->pixelMetric(QStyle::PM_DefaultTopLevelMargin);
int x = width() - margin;
int y = height() - margin;

y = updateButtonGeometry(newCircleButton, x, y);
y = updateButtonGeometry(newArrowButton, x, y);


}

void SortingBox::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);

foreach (ShapeItem shapeItem, shapeItems)
{
painter.translate(shapeItem.position());
painter.setBrush(shapeItem.color());
painter.drawPath(shapeItem.path());
painter.translate(-shapeItem.position());
}

QPoint midpoint;
QPoint test(100,100);
if(lineCheck >1)
{
for(int i=0;i<count-1;i= i+2)
{
/*for(int j=j+1;j<count;j++)
{*/

painter.setPen(initialItemColor() );

painter.drawLine( points1[i], points1[i+1] );
midpoint = ((points1[i] + points1[i+1])/2);

QSize textbox(100,100);
QString str = "50";

if(clearer = 1 && lineCheck>2)
{

painter.setPen(myTextColor());
painter.drawText(QRect(midpoint,textbox),0,tr("%1").arg(str));


}update();
} update();

}
}



void SortingBox::mousePressEvent(QMouseEvent *event)
{
down = FALSE;

if (event->button() == Qt::LeftButton) {
int index = itemAt(event->pos());
if (index != -1) {
itemInMotion = &shapeItems[index];
previousPosition = event->pos();
shapeItems.move(index, shapeItems.size() - 1);
update();
recordLine = true;


points1[count++] = QPoint(event->pos()); // add point

lineCheck = lineCheck +1;

}
else
createShapeItem(circlePath, tr("Node"), QPoint(event->pos()),
randomItemColor());
recordLine = false;
}

}

void SortingBox::mouseMoveEvent(QMouseEvent *event)
{
if ((event->buttons() & Qt::LeftButton) && itemInMotion)
moveItemTo(event->pos());

if(recordLine == true)
{
if ( down && count < MAXPOINTS ) {
QPainter painter( this );
points1[count++] = event->pos(); // add point

}
}
}

void SortingBox::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton && itemInMotion) {
moveItemTo(event->pos());
itemInMotion = 0;
}
down = FALSE;
update();
if(recordLine == true)
{ if (count < MAXPOINTS ) {
QPainter painter( this );
points1[count++] = QPoint(event->pos());

}
}

}


int SortingBox::itemAt(const QPoint &pos)
{
for (int i = shapeItems.size() - 1; i >= 0; --i) {
const ShapeItem &item = shapeItems[i];
if (item.path().contains(pos - item.position()))
return i;
}
return -1;
}



int SortingBox::updateButtonGeometry(QToolButton *button, int x, int y)
{
QSize size = button->sizeHint();
button->setGeometry(x - size.rwidth(), y - size.rheight(),
size.rwidth(), size.rheight());

return y - size.rheight()
- style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing);
}

void SortingBox::createShapeItem(const QPainterPath &path,
const QString &toolTip, const QPoint &pos,
const QColor &color)
{
ShapeItem shapeItem;
shapeItem.setPath(path);
shapeItem.setToolTip(toolTip);
shapeItem.setPosition(pos);
shapeItem.setColor(color);
shapeItems.append(shapeItem);
update();
}

QToolButton *SortingBox::createToolButton(const QString &toolTip,
const QIcon &icon, const char *member)
{
QToolButton *button = new QToolButton(this);
button->setToolTip(toolTip);
button->setIcon(icon);
button->setIconSize(QSize(30, 30));
connect(button, SIGNAL(clicked()), this, member);

return button;
}

QPoint SortingBox::initialItemPosition(const QPainterPath &path)
{
int x;
int y = (height() - (int)path.controlPointRect().height()) / 2;
if (shapeItems.size() == 0)
x = ((3 * width()) / 2 - (int)path.controlPointRect().width()) / 2;
else
x = (width() / shapeItems.size()
- (int)path.controlPointRect().width()) / 2;

return QPoint(x, y);
}

QPoint SortingBox::randomItemPosition()
{
return QPoint(rand() % (width() - 120), rand() % (height() - 120));
}

QColor SortingBox::initialItemColor()
{
return QColor::fromHsv(256 % 256, 255, 190);
}

QColor SortingBox::randomItemColor()
{
return QColor::fromHsv(200 % 256, 255, 190);//256 % 256 makes red//

}

QColor SortingBox::myTextColor()
{
return QColor::fromHsv(235 % 256, 255, 190);//256 % 256 makes red//
}

void SortingBox::clear()
{
clearer = 1;
}

---------------------------------------------------------------------------------------------------

wysota
12th February 2006, 11:21
Use a static field in your item class to denote how many instances of the class were created and store the number of an instance in the instance itself, like so:


class Item{
Item(){
static uint __inst=0;
_instance_no = __inst++;
}
uint instanceNumber(){ return _instance_no; }
private:
uint _instance_no;
};