]> Shamusworld >> Repos - virtualjaguar/commitdiff
Incremental improvements to the file picker, introduction of ImageDelegate
authorShamus Hammons <jlhamm@acm.org>
Mon, 8 Feb 2010 06:09:52 +0000 (06:09 +0000)
committerShamus Hammons <jlhamm@acm.org>
Mon, 8 Feb 2010 06:09:52 +0000 (06:09 +0000)
class to do custom rendering for our QListView

Makefile
src/gui/filepicker.cpp
src/gui/filepicker.h
src/gui/filethread.cpp
src/gui/imagedelegate.cpp [new file with mode: 0644]
src/gui/imagedelegate.h [new file with mode: 0644]
src/gui/mainwin.cpp
src/gui/vj.qrc

index 1bc442f9ba52180725ddaa7b79e0fb8bb05380cf..c4a771f55745b5be862cd2ea79f7919118f24d24 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -79,6 +79,7 @@ OBJS = \
        obj/moc_filepicker.o \
        obj/filelistmodel.o \
        obj/filethread.o    \
+       obj/imagedelegate.o \
        obj/moc_filethread.o \
        obj/mainwin.o       \
        obj/moc_mainwin.o   \
index e8a44d1bad7ea9d88d32fb99bd71fa06170afdff..52117362d37b98184392f298864bab397efeda1f 100644 (file)
@@ -9,6 +9,7 @@
 // Who  When        What
 // ---  ----------  -------------------------------------------------------------
 // JLH  01/22/2010  Created this file
+// JLH  02/06/2010  Modified to use Qt model/view framework
 //
 
 #include "filepicker.h"
@@ -16,6 +17,7 @@
 #include "crc32.h"
 #include "filelistmodel.h"
 #include "filethread.h"
+#include "imagedelegate.h"
 #include "settings.h"
 #include "types.h"
 
@@ -113,7 +115,7 @@ FilePickerWindow::FilePickerWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt:
 {
        setWindowTitle("Insert Cartridge...");
 
-#if 1
+#if 0
        fileList2 = new QListWidget(this);
 //     addWidget(fileList);
 
@@ -131,13 +133,20 @@ FilePickerWindow::FilePickerWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt:
 
        fileThread->Go(fileList2);
 #else
-QStringList numbers;
-numbers << "One" << "Two" << "Three" << "Four" << "Five";
-
-QAbstractItemModel * model = new StringListModel(numbers);
-QListView * view = new QListView;
-view->setModel(model);
+//is there any reason why this must be cast as a QAbstractListModel?
+//Also, need to think about data structure for the model...
+       model = new FileListModel();
+       fileList = new QListView();
+       fileList->setModel(model);
+       ImageDelegate * delegate = new ImageDelegate(this);
+       fileList->setItemDelegate(delegate);
+
+       QVBoxLayout * layout = new QVBoxLayout;
+       setLayout(layout);
 
+       layout->addWidget(fileList);
+       ((FileListModel *)model)->AddData(QIcon(":/res/generic.png"));
+       ((FileListModel *)model)->AddData(QIcon(":/res/generic.png"));
 #endif
 }
 
@@ -145,7 +154,7 @@ view->setModel(model);
 
 void FilePickerWindow::AddFileToList(unsigned long index)
 {
-       printf("--> Found CRC: %08X...\n", (uint32)index);
+       printf("--> Found CRC: %08X...\n", romList2[index].crc32);
 }
 
 
index e11dc34777729f31b3b51f40c165dfc1f30ed3ec..543de0ebeba39c3a39039fd8c798275b3f4fd341 100644 (file)
@@ -8,7 +8,7 @@
 // Forward declarations
 class QListWidget;
 class FileThread;
-class FileListModel;
+//class FileListModel;
 class QListView;
 
 class FilePickerWindow: public QWidget
@@ -28,6 +28,6 @@ class FilePickerWindow: public QWidget
        private:
                QListWidget * fileList2;
                FileThread * fileThread;
-               FileListModel * model;
+               QAbstractItemModel * model;
                QListView * fileList;
 };
index 8bb2b87b70f4edda23d8acd3369b9ebc136ee784..7ecb4532abc25e6d7c1d763511e423ddd3e50faa 100644 (file)
@@ -167,7 +167,8 @@ printf("FileThread: Aborting!!!\n");
                        {
 printf("FileThread: Found match [%s]...\n", romList[index].name);
                                new QListWidgetItem(QIcon(":/res/generic.png"), romList[index].name, listWidget);
-                               emit FoundAFile(romList[index].crc32);
+//                             emit FoundAFile(romList[index].crc32);
+                               emit FoundAFile(index);
                        }
                }
 
diff --git a/src/gui/imagedelegate.cpp b/src/gui/imagedelegate.cpp
new file mode 100644 (file)
index 0000000..76e690b
--- /dev/null
@@ -0,0 +1,106 @@
+//
+// imagedelegate.cpp - Qt Model/View rendering class
+//
+// by James L. Hammons
+// (C) 2010 Underground Software
+//
+// JLH = James L. Hammons <jlhamm@acm.org>
+//
+// Who  When        What
+// ---  ----------  -------------------------------------------------------------
+// JLH  02/04/2010  Created this file
+//
+
+// This class takes care of rendering items in our custom model in the ListView
+// class utilized in FilePicker.
+
+#include "imagedelegate.h"
+
+
+ImageDelegate::ImageDelegate(QObject * parent): QAbstractItemDelegate(parent), pixelSize(12)
+{
+}
+
+/*
+Each item is rendered by the delegate's paint() function. The view calls this function with a ready-to-use QPainter object, style information that the delegate should use to correctly draw the item, and an index to the item in the model:
+*/
+
+void ImageDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
+{
+       if (option.state & QStyle::State_Selected)
+               painter->fillRect(option.rect, option.palette.highlight());
+
+/*
+The first task the delegate has to perform is to draw the item's background correctly. Usually, selected items appear differently to non-selected items, so we begin by testing the state passed in the style option and filling the background if necessary.
+
+The radius of each circle is calculated in the following lines of code:
+*/
+
+#if 0
+       int size = qMin(option.rect.width(), option.rect.height());
+       int brightness = index.model()->data(index, Qt::DisplayRole).toInt();
+       double radius = (size/2.0) - (brightness/255.0 * size/2.0);
+       if (radius == 0.0)
+               return;
+#endif
+
+/*
+First, the largest possible radius of the circle is determined by taking the smallest dimension of the style option's rect attribute. Using the model index supplied, we obtain a value for the brightness of the relevant pixel in the image. The radius of the circle is calculated by scaling the brightness to fit within the item and subtracting it from the largest possible radius.
+*/
+
+       painter->save();
+#if 0
+       painter->setRenderHint(QPainter::Antialiasing, true);
+       painter->setPen(Qt::NoPen);
+
+/*
+We save the painter's state, turn on antialiasing (to obtain smoother curves), and turn off the pen.
+*/
+
+       if (option.state & QStyle::State_Selected)
+               painter->setBrush(option.palette.highlightedText());
+       else
+               painter->setBrush(QBrush(Qt::black));
+
+/*
+The foreground of the item (the circle representing a pixel) must be rendered using an appropriate brush. For unselected items, we will use a solid black brush; selected items are drawn using a predefined brush from the style option's palette.
+*/
+
+       painter->drawEllipse(QRectF(option.rect.x() + option.rect.width()/2 - radius,
+               option.rect.y() + option.rect.height()/2 - radius, 2*radius, 2*radius));
+#else
+//     painter->drawPixmap(option.rect.x()+8, option.rect.y()+8, 200, 94, QPixmap(":/res/labels/rayman.jpg"));
+       painter->drawPixmap(option.rect.x(), option.rect.y(), 488/2, 395/2, QPixmap(":/res/cart-blank.png"));
+       painter->drawPixmap(option.rect.x()+13, option.rect.y()+51, 433/2, 203/2, QPixmap(":/res/labels/rayman.jpg"));
+//26x100
+#endif
+       painter->restore();
+}
+
+/*
+Finally, we paint the circle within the rectangle specified by the style option and we call restore() on the painter.
+
+The paint() function does not have to be particularly complicated; it is only necessary to ensure that the state of the painter when the function returns is the same as it was when it was called. This usually means that any transformations applied to the painter must be preceded by a call to QPainter::save() and followed by a call to QPainter::restore().
+
+The delegate's sizeHint() function returns a size for the item based on the predefined pixel size, initially set up in the constructor:
+*/
+
+QSize ImageDelegate::sizeHint(const QStyleOptionViewItem & /* option */, const QModelIndex & /* index */) const
+{
+       // 488x395 --> blank cart (full size)
+       // 400x188 --> label (full size) 433x203 <-- (actually, it's this)
+
+       // 200x94 is shrunk dimension...
+//     return QSize(100, 47);
+//     return QSize(216, 110);
+       return QSize(488/2, 395/2);
+}
+
+/*
+The delegate's size is updated whenever the pixel size is changed. We provide a custom slot to do this:
+*/
+
+//void ImageDelegate::setPixelSize(int size)
+//{
+//     pixelSize = size;
+//}
diff --git a/src/gui/imagedelegate.h b/src/gui/imagedelegate.h
new file mode 100644 (file)
index 0000000..78d07dc
--- /dev/null
@@ -0,0 +1,27 @@
+//
+// imagedelegate.h: Class definition
+//
+
+#ifndef __IMAGEDELEGATE_H__
+#define __IMAGEDELEGATE_H__
+
+#include <QtGui>
+
+class ImageDelegate: public QAbstractItemDelegate
+{
+//     Q_OBJECT
+
+       public:
+               ImageDelegate(QObject * parent = 0);
+
+               void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
+               QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index ) const;
+
+//     public slots:
+//             void setPixelSize(int size);
+
+       private:
+               int pixelSize;
+};
+
+#endif // __IMAGEDELEGATE_H__
index bbeb3f867dd045aa89ac0c03fe4f6ef9aaa16ff4..3d9276f3d77d309a28cacc285acb271b1a635bec 100644 (file)
@@ -110,7 +110,7 @@ MainWin::MainWin()
        aboutAct->setStatusTip(tr("Blatant self-promotion"));
        connect(aboutAct, SIGNAL(triggered()), this, SLOT(ShowAboutWin()));
 
-       filePickAct = new QAction(QIcon(":/res/generic.png"), tr("Insert Cartridge..."), this);
+       filePickAct = new QAction(QIcon(":/res/generic.png"), tr("&Insert Cartridge..."), this);
        filePickAct->setStatusTip(tr("Insert a cartridge into Virtual Jaguar"));
        connect(filePickAct, SIGNAL(triggered()), this, SLOT(InsertCart()));
 
index 4a23d8c9ed98e08acb4f67e8d487b10ed3e90a30..f55a393b843828ce0e6f29ef5f16bc09d9a4633f 100644 (file)
@@ -6,6 +6,8 @@
        <file>../../res/zoom200.png</file>
        <file>../../res/zoom300.png</file>
        <file>../../res/generic.png</file>
+       <file>../../res/cart-blank.png</file>
        <file>../../res/vj_title_small.png</file>
+       <file>../../res/labels/rayman.jpg</file>
 </qresource>
 </RCC>