Changeset 8480:4bcdfff7fe1d in orange


Ignore:
Timestamp:
07/27/11 13:29:32 (3 years ago)
Author:
Noughmad <Noughmad@…>
Branch:
default
Convert:
68ef9ecbd83e97000436e6371a228c30bd32df59
Message:

Create a global point cache that stores QPixmaps

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • orange/OrangeWidgets/plot/owplot.py

    r8479 r8480  
    10281028    def update_antialiasing(self): 
    10291029        self.setRenderHint(QPainter.Antialiasing, self.use_antialiasing) 
     1030        orangeplot.Point.clear_cache() 
  • source/orangeplot/point.cpp

    r8476 r8480  
    66 
    77const int ChangeableColorIndex = 0; 
     8 
     9QHash<PointData, QPixmap> Point::pixmap_cache; 
     10 
     11uint qHash(const PointData& data) 
     12{ 
     13    // uint has 32 bits: 
     14    uint ret = data.size; 
     15    // size only goes up to 20, so 5 bits is enough 
     16    ret |= data.symbol << 5; 
     17    // symbol is less than 16, so 4 bits will do 
     18    ret |= data.state << 9; 
     19    // QRgb takes the full uins, so we just XOR by it 
     20    ret ^= data.color.rgba(); 
     21    return ret; 
     22} 
     23 
     24bool operator==(const PointData& one, const PointData& other) 
     25{ 
     26    return one.symbol == other.symbol && one.size == other.size && one.state == other.state && one.color == other.color; 
     27} 
     28 
    829 
    930Point::Point(int symbol, QColor color, int size, QGraphicsItem* parent): QGraphicsItem(parent), 
     
    2950    Q_UNUSED(widget) 
    3051     
    31     if (m_display_mode == DisplayPath) 
    32     { 
    33         if (m_state & Selected) 
     52    const PointData key(m_size, m_symbol, m_color, m_state); 
     53    // We make the pixmap slighly larger because the point outline has non-zero width 
     54    const int ps = m_size + 4; 
     55    if (!pixmap_cache.contains(key)) 
     56    { 
     57        if (m_display_mode == DisplayPath) 
    3458        { 
    35             painter->setBrush(m_color); 
    36             painter->setPen(Qt::yellow); 
     59            QPixmap pixmap(ps, ps); 
     60            pixmap.fill(Qt::transparent); 
     61            QPainter p; 
     62            p.begin(&pixmap); 
     63            p.setRenderHints(painter->renderHints()); 
     64            if (m_state & Selected) 
     65            { 
     66                p.setBrush(m_color); 
     67                p.setPen(Qt::yellow); 
     68            } 
     69            else if (m_state & Marked) 
     70            { 
     71                p.setBrush(m_color); 
     72            } 
     73            else 
     74            { 
     75                p.setPen(m_color); 
     76            } 
     77            const QPainterPath path = path_for_symbol(m_symbol, m_size); 
     78            p.drawPath(path.translated(0.5*ps, 0.5*ps)); 
     79            pixmap_cache.insert(key, pixmap); 
     80        }  
     81        else if (m_display_mode == DisplayPixmap) 
     82        { 
     83            pixmap_cache.insert(key, pixmap_for_symbol(m_symbol, m_color, m_size)); 
    3784        } 
    38         else if (m_state & Marked) 
    39         { 
    40             painter->setBrush(m_color); 
    41         } 
    42         else 
    43         { 
    44             painter->setPen(m_color); 
    45         } 
    46         const QPainterPath path = path_for_symbol(m_symbol, m_size); 
    47         painter->drawPath(path_for_symbol(m_symbol, m_size)); 
    48     }  
    49     else if (m_display_mode == DisplayPixmap) 
    50     { 
    51         QImage image = image_for_symbol(m_symbol, m_color, m_size); 
    52         image.setColor(ChangeableColorIndex, m_color.rgb()); 
    53         painter->drawImage(boundingRect(), image); 
    54     } 
     85    } 
     86    painter->drawPixmap(QPointF(-0.5*ps, -0.5*ps), pixmap_cache.value(key)); 
    5587} 
    5688 
     
    223255} 
    224256 
    225 QImage Point::image_for_symbol(int symbol, QColor color, int size) 
     257QPixmap Point::pixmap_for_symbol(int symbol, QColor color, int size) 
    226258{ 
    227259    // Indexed8 is the only format with a color table, which means we can replace entire colors 
    228260    // and not only indididual pixels 
    229     QImage image(QSize(size, size), QImage::Format_Indexed8); 
     261    QPixmap image(QSize(size, size)); 
    230262     
    231263    // TODO: Create fils with actual images, preferably SVG so they are scalable 
     
    280312} 
    281313 
    282  
    283  
     314void Point::clear_cache() 
     315{ 
     316    pixmap_cache.clear(); 
     317} 
     318 
     319 
     320 
     321 
  • source/orangeplot/point.h

    r8479 r8480  
    33 
    44#include <QtGui/QGraphicsItem> 
     5 
     6struct PointData 
     7{ 
     8    PointData(int size, int symbol, const QColor& color, int state) : size(size), symbol(symbol), color(color), state(state) {} 
     9    int size; 
     10    int symbol; 
     11    QColor color; 
     12    int state; 
     13}; 
     14 
     15uint qHash(const PointData& data); 
     16bool operator==(const PointData& one, const PointData& other); 
    517 
    618class Point : public QGraphicsItem 
     
    99111    static QPainterPath path_for_symbol(int symbol, int size); 
    100112     
    101     static QImage image_for_symbol(int symbol, QColor color, int size); 
     113    static QPixmap pixmap_for_symbol(int symbol, QColor color, int size); 
    102114    static QRectF rect_for_size(double size); 
     115     
     116    static void clear_cache(); 
    103117         
    104118private: 
     
    106120    static QPainterPath crossPath(double d, double rot); 
    107121    static QPainterPath hexPath(double d, bool star); 
     122     
     123    static QHash<PointData, QPixmap> pixmap_cache; 
    108124 
    109125    int m_symbol; 
  • source/orangeplot/point.sip

    r8479 r8480  
    8282    static QPainterPath path_for_symbol(int symbol, int size); 
    8383     
    84     static QImage image_for_symbol(int symbol, QColor color, int size); 
     84    static QPixmap pixmap_for_symbol(int symbol, QColor color, int size); 
     85    static void clear_cache(); 
    8586}; 
Note: See TracChangeset for help on using the changeset viewer.