QListWidget visual representation

Started by nouvakis, April 21, 2012, 09:19:47 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

nouvakis

hello,

I've played with QItemDelegate and I propose this class

CODE START ==============================================
#ifndef MYTEACHERDELEGATE_H
#define MYTEACHERDELEGATE_H

#include <QListWidget>
#include <QAbstractItemView>
#include <QItemDelegate>
#include <qrect.h>
#include <qsize.h>
#include <qpainter.h>

#include <QObject>
#include <QMetaObject>

class MyTeacherDelegate : public QItemDelegate
{
    Q_OBJECT
public:
    inline MyTeacherDelegate(QObject *parent=0) : QItemDelegate(parent) {}

    void MyTeacherDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,  const QModelIndex &index ) const
    {
        if (option.state & QStyle::State_Selected)
        {
            painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
        }
        else {
            if (index.row() % 2) // even rows different color
                painter->fillRect(option.rect, QColor(225,225,225));
        }
        QString name = QString::number( index.row()+1 ).append(".");

        QRect r = option.rect;
        r.setWidth(24);
        painter->drawText(r.left(), r.top(), r.width(), r.height(),
                          Qt::AlignVCenter|Qt::AlignRight, name, &r);

        r = option.rect.adjusted(34,0,0,0);
        name = index.model()->data(index).toString();
        painter->drawText(r.left(), r.top(), r.width(), r.height(),
                          Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap, name, &r);
    }
};

#endif
CODE ENDS ==============================================

you can use it in teachersform.cpp (or for all QListWidget in project)

CODE START ==============================================
....
TeachersForm::TeachersForm(QWidget* parent): QDialog(parent)
{
setupUi(this);

currentTeacherTextEdit->setReadOnly(true);

renameTeacherPushButton->setDefault(true);

teachersListWidget->setSelectionMode(QAbstractItemView::SingleSelection);

        // this is the use of Delegate   <-----
        teachersListWidget->setItemDelegate(new MyTeacherDelegate(teachersListWidget));
...
CODE ENDS ==============================================

a photo of Teachers Form using this code is attached

regards Petros

Liviu Lalescu

Nice!

Let us hear what other users think.

To set alternate rows colors, I think there is a single option for each QListWidget. But indeed, to add numbers before the name I think you need to use your method.

Hmm, but if the user sorts the teachers, the numbers are changed. The user must understand a number does not represent a teacher, but only the position count.

nouvakis

I've created another version of QListWidget visual representation for teachers. I think it would be very helpful if next to teacher's name, there would be an icon showing which days the teacher is (or isn't) available !!!

I've attached the delegate class (myteacherdelegate.h, myteacherdelegate.cpp) and
teachersform.cpp with comments showing where my code starts and where it ends.

regards petros

Liviu Lalescu

#3
Well, that looks really good.

1) #include <qpainter.h> should be #include <QPainter> and so on.

2) You are doing a too much time consuming loop: n_teachers*n_constraints. I think the best way would be to have a list of pointers to constraints for each teacher, to compute once before processing, and then it would be faster. I mean have an n_teachers+n_constraints processing time.

You could use for instance a Matrix1D[n_teachers] or a QHash<teacher_name, constraint_pointer> and loop through each constraint and update these. Or even have a new variable in rules.h, to keep time constraints for each teacher and update it when adding/removing constraints. But it is more complicated.

I admit my old code is not entirely optimized like above. Reasons: it is old, and I didn't think from the start at this.

3) What if the number of days per week is 35 and hours per day is 60?

4) Yes, your facilities are really nice. But:
     a) Each code in FET needs to be tested and checked very thoroughly. It is critical code. I would prefer to check each code addition personally.
     b) Unfortunately, there are possible many improvements, but I don't have time to dedicate myself to this.
     c) I prefer to keep FET to a very small core (in fact, I would like even not to have an interface, but it is necessary for a minimum work).
     d) You are of course encouraged to develop a nicer interface, in the way you began. But the easiest way for me would be to have it as an additional program, or alternative program, and I'll put links to it on the FET homepage and if users consider it is good, it may become a preferred download.

Please let me know what you think.

nouvakis

I've installed QT a week ago, and your project is too big to read all the code. So, my code may look a little "amatuer" right now.

Quote1) #include <qpainter.h> should be #include <QPainter> and so on.
I did this after I've posted the code !

Quote2) You are doing a too much time consuming loop: n_teachers*n_constraints. I think the best way would be to have a list of pointers to constraints for each teacher, to compute once before processing, and then it would be faster. I mean have an n_teachers+n_constraints processing time.

You could use for instance a Matrix1D[n_teachers] or a QHash<teacher_name, constraint_pointer> and loop through each constraint and update these. Or even have a new variable in rules.h, to keep time constraints for each teacher and update it when adding/removing constraints. But it is more complicated.

I admit my old code is not entirely optimized like above. Reasons: it is old, and I didn't think from the start at this.
For this part I looked your code. You had a similar loop (in two or more files). I should read your code and learn the structures you use to make it better. But the processing time is not a problem, because it is used just once before the form with a QListWidget shows !

Quote3) What if the number of days per week is 35 and hours per day is 60?
I've attached an image of this case

Quote4) Yes, your facilities are really nice. But:
     a) Each code in FET needs to be tested and checked very thoroughly. It is critical code. I would prefer to check each code addition personally.
you are right

Quoteb) Unfortunately, there are possible many improvements, but I don't have time to dedicate myself to this.
We had 2 weeks off and I had time to spear. Maybe I could help, but I have to learn QT library better.

Quoted) You are of course encouraged to develop a nicer interface, in the way you began. But the easiest way for me would be to have it as an additional program, or alternative program, and I'll put links to it on the FET homepage and if users consider it is good, it may become a preferred download.
Well, I could make a better interface only for Windows right now. I need more time to learn QT for cross platform software.

regards

Liviu Lalescu

#5
Quote from: nouvakis on April 22, 2012, 09:48:58 AM
I've installed QT a week ago, and your project is too big to read all the code. So, my code may look a little "amatuer" right now.

No problem. I also began with amateur code.

FET is big, but keep in mind that all constraints dialogs are copy/paste code. If you remove them, the rest is more manageable. And much code is copy/paste  :-[

Quote
For this part I looked your code. You had a similar loop (in two or more files). I should read your code and learn the structures you use to make it better. But the processing time is not a problem, because it is used just once before the form with a QListWidget shows !

Yes, I might have a loop, but generally I avoid n_teachers*n_constraints loops (otherwise, it is a mistake, which I should correct, but I am generally afraid to change old code, to not introduce new bugs).

My loop may be for only one teacher.

Yes, 100 teachers * 1000 constraints is not much, but it is not perfect.

Quote
I've attached an image of this case

Maybe add a Boolean option for this?

Quote
Well, I could make a better interface only for Windows right now. I need more time to learn QT for cross platform software.

But you already coded in Qt :-)

Qt is a good creation, worth to be learned. I don't know about the new QML and declarative - I still use QWidgets.

nouvakis

QuoteMaybe add a Boolean option for this?
.

I will do it today and I will send my "upgraded !" code.

Liviu Lalescu

OK!

PS: There is a code marking in this forum, see the # sign. I modified your first post. I hope you agree.

Volker Dirr

Quote from: nouvakis on April 22, 2012, 09:48:58 AM
Quote3) What if the number of days per week is 35 and hours per day is 60?
I've attached an image of this case

That is in my opinion to hugh.
but i think there is an easy solution:
if there are many rows and columns (maybe if the number is > 10), then:
a) reduce the size of your X_BOX and/or Y_BOX
b) remove the black lines that split the boxes. if there are so many rows and columns, then you can't count them, so you can also remove them. you can still edit that constraint to have an exact view.

nouvakis

hello again,
I think that I've finished my code. If you are interested, you can use it.

I've tried a boolean parameter (user's choise on the menu) just like USE_GUI_COLORS.
Maybe you would like to place a checkbox in every teachers Widget list (I didn't try this)

regards Petros

Liviu Lalescu

Thank you!

I will consider this, but I cannot promise.

I see that population is still n_teachers*n_constraints, which is not perfect.

Do you have a homepage, so I'll put links to it, and you'll put your code there? (for more visibility - there is of course no problem if your code is also on the forum, but not many may check this topic).

nouvakis

#11
Quote from: Liviu Lalescu on April 29, 2012, 06:14:06 AM
I see that population is still n_teachers*n_constraints, which is not perfect.
The reason of this kind of code is:
a) it is used just once (form initialization)
b) to make it faster, I should
  i) construct first another list of pointers to Constraints of type CONSTRAINT_TEACHER_NOT_AVAILABLE_TIMES (something like a filter) or
  ii) look for another solution (do you have any pointers to Teacher's constraints in the Teacher Object ?)

I have a homepage (just to declare my existance - nothing serious - it's on Greek School Network which isn't online right now !!!)
http://users.sch.gr/nouvakis

PS: my homepage is in Greek language

Liviu Lalescu

b) i) is what I suggest :-)  A list of constraint pointers for each teacher, to be constructed even each time the form is started, because with a QHash you get linear time.

Yes, b) ii) is also good, but too late for me to change now. Maybe in the future.

For me to list your homepage or portion of homepage related to FET, you need to put there your work for download.

Volker Dirr

#13
I just want to say that this code should use QImage instead of QPixmap. (Compare Qt-Documentation. QImage is slower, but avoid problems with mac-os and x-server. Also if you want to use non GUI-version on a server without a GPU. This "icons" are small and static, so the lost speed is not critical. But the disadvantages of QPixmap might be very critical. Compare Qt-Documentation.).