Don't reload the pixmaps every time they are displayed... -.-

This commit is contained in:
dfighter1985 2014-08-01 02:34:57 +02:00
parent 2ddd7e1035
commit 66c8b95e2d
3 changed files with 59 additions and 12 deletions

View file

@ -264,23 +264,71 @@ void TileTypeNode::reindex()
/////////////////////////////////////////////////// ///////////////////////////////////////////////////
class TileItemNodePvt
{
public:
bool loadImage( TileModel::TTileChannel channel, const QString &fn )
{
QPixmap temp;
bool b = temp.load( fn );
if( !b )
return false;
pixmaps[ channel ] = temp;
return true;
}
void clearImage( TileModel::TTileChannel channel )
{
pixmaps[ channel ] = QPixmap();
}
const QPixmap& pixMap( TileModel::TTileChannel channel ) const{
return pixmaps[ channel ];
}
private:
QPixmap pixmaps[ TileModel::TileChannelCount ];
};
TileModel::TTileChannel TileItemNode::s_displayChannel = TileModel::TileDiffuse; TileModel::TTileChannel TileItemNode::s_displayChannel = TileModel::TileDiffuse;
TileItemNode::TileItemNode(int tileId, TileModel::TTileChannel channel, QString filename, Node *parent) : m_tileId(tileId) TileItemNode::TileItemNode(int tileId, TileModel::TTileChannel channel, QString filename, Node *parent) : m_tileId(tileId)
{ {
m_tileFilename[channel] = filename;
m_parentItem = parent; m_parentItem = parent;
//nlinfo("dispalying tile %d - %s", m_tileId, m_tileFilename[TileModel::TileDiffuse].toAscii().data()); //nlinfo("dispalying tile %d - %s", m_tileId, m_tileFilename[TileModel::TileDiffuse].toAscii().data());
pvt = new TileItemNodePvt();
setTileFilename( channel, filename );
} }
TileItemNode::~TileItemNode() TileItemNode::~TileItemNode()
{ {
delete pvt;
pvt = NULL;
qDeleteAll(m_childItems); qDeleteAll(m_childItems);
} }
void TileItemNode::setTileFilename(TileModel::TTileChannel channel, QString filename) bool TileItemNode::setTileFilename(TileModel::TTileChannel channel, QString filename)
{ {
QString fn = filename;
if( filename.isEmpty() || ( filename == "empty" ) )
fn = ":/placeHolder/images/empty_image.png";
bool b = pvt->loadImage( channel, fn );
if( !b )
return false;
m_tileFilename[channel] = filename; m_tileFilename[channel] = filename;
return true;
} }
QString TileItemNode::getTileFilename(TileModel::TTileChannel channel) QString TileItemNode::getTileFilename(TileModel::TTileChannel channel)
@ -295,8 +343,7 @@ QString TileItemNode::getTileFilename(TileModel::TTileChannel channel)
QVariant TileItemNode::data(int column, int role) const QVariant TileItemNode::data(int column, int role) const
{ {
// find some way to know which file/bitmap to display QString tileFilename = m_tileFilename[ TileItemNode::s_displayChannel ];
QString tileFilename = m_tileFilename[s_displayChannel];
if(role == TileModel::TilePixmapRole || role == Qt::DecorationRole) if(role == TileModel::TilePixmapRole || role == Qt::DecorationRole)
{ {
@ -307,18 +354,13 @@ QVariant TileItemNode::data(int column, int role) const
// Retrieve the target tile size. // Retrieve the target tile size.
uint32 tileSize = TileModel::getTileTypeSize(parent->getTileType()); uint32 tileSize = TileModel::getTileTypeSize(parent->getTileType());
if(tileFilename.isEmpty() || tileFilename == "empty")
tileFilename = ":/placeHolder/images/empty_image.png";
QPixmap pixmap;// = new QPixmap();
if(!pixmap.load(tileFilename))
nlinfo("failed to load %s", tileFilename.toAscii().data());
if(TileModel::CurrentZoomFactor == TileModel::TileZoom200) if(TileModel::CurrentZoomFactor == TileModel::TileZoom200)
tileSize *= 2; tileSize *= 2;
else if(TileModel::CurrentZoomFactor == TileModel::TileZoom50) else if(TileModel::CurrentZoomFactor == TileModel::TileZoom50)
tileSize /= 2; tileSize /= 2;
QPixmap pixmap = pvt->pixMap( TileItemNode::s_displayChannel );
pixmap.scaled(tileSize, tileSize); pixmap.scaled(tileSize, tileSize);
return pixmap; return pixmap;

View file

@ -104,6 +104,8 @@ private:
TileModel::TNodeTileType m_nodeTileType; TileModel::TNodeTileType m_nodeTileType;
}; };
class TileItemNodePvt;
class TileItemNode : public Node class TileItemNode : public Node
{ {
public: public:
@ -111,7 +113,7 @@ public:
virtual ~TileItemNode(); virtual ~TileItemNode();
QVariant data(int column, int role) const; QVariant data(int column, int role) const;
int columnCount() const; int columnCount() const;
void setTileFilename(TileModel::TTileChannel channel, QString filename); bool setTileFilename(TileModel::TTileChannel channel, QString filename);
QString getTileFilename(TileModel::TTileChannel channel); QString getTileFilename(TileModel::TTileChannel channel);
void setId( int id ){ m_tileId = id; } void setId( int id ){ m_tileId = id; }
int id() const{ return m_tileId; } int id() const{ return m_tileId; }
@ -125,6 +127,8 @@ private:
QMap<TileModel::TTileChannel, TileWidget*> m_tileWidget; QMap<TileModel::TTileChannel, TileWidget*> m_tileWidget;
static TileModel::TTileChannel s_displayChannel; static TileModel::TTileChannel s_displayChannel;
TileItemNodePvt *pvt;
}; };
#endif // TILE_ITEM_H #endif // TILE_ITEM_H

View file

@ -34,6 +34,7 @@ public:
TileDiffuse = 0, TileDiffuse = 0,
TileAdditive = 1, TileAdditive = 1,
TileAlpha = 2, TileAlpha = 2,
TileChannelCount = 3
}; };
enum TNodeTileType enum TNodeTileType