Qwt源碼解讀之QwtText類和QwtTextLabel類

來源:互聯網
上載者:User

QwtText類表徵Qwt中的一段文本。與QString相比,QwtText包含了一系列怎樣渲染文本的屬性特徵。通過指定這些屬性,你可以繪製各種各樣的文本樣式。

程式碼分析:

1、介面樣本:

    double heightForWidth( double width, const QFont & = QFont() ) const;  // 根據指定的寬度給出最合適的高度    QSizeF textSize( const QFont & = QFont() ) const;    void draw( QPainter *painter, const QRectF &rect ) const; // 在一個矩形裡繪製常值內容    static const QwtTextEngine *textEngine(         const QString &text, QwtText::TextFormat = AutoText );    static const QwtTextEngine *textEngine( QwtText::TextFormat );    static void setTextEngine( QwtText::TextFormat, QwtTextEngine * );

double heightForWidth( double width, const QFont & = QFont() ) const;   heightForWidth() 一個好的介面命名樣本。

2、建構函式:

/*!   Constructor   \param text Text content   \param textFormat Text format*/QwtText::QwtText( const QString &text, QwtText::TextFormat textFormat ){    d_data = new PrivateData;    d_data->text = text;    d_data->textEngine = textEngine( text, textFormat );    d_layoutCache = new LayoutCache;}

3、拷貝建構函式:

//! Copy constructorQwtText::QwtText( const QwtText &other ){    d_data = new PrivateData; // 分配記憶體空間    *d_data = *other.d_data;  // 賦值    d_layoutCache = new LayoutCache; // 分配記憶體空間    *d_layoutCache = *other.d_layoutCache;  // 賦值}

4、拷貝賦值操作符函數:

//! Assignment operatorQwtText &QwtText::operator=( const QwtText & other ){    *d_data = *other.d_data;    *d_layoutCache = *other.d_layoutCache;    return *this;}

5、解構函式:

//! DestructorQwtText::~QwtText(){    delete d_data;    delete d_layoutCache;}

6、“文本引擎”提供者:

/*!   \brief Find the text engine for a text format   textEngine can be used to find out if a text format is supported.   \param format Text format   \return The text engine, or NULL if no engine is available.*/const QwtTextEngine *QwtText::textEngine( QwtText::TextFormat format ){    return  QwtTextEngineDict::dict().textEngine( format );}

共有兩個“文本引擎”提供者,我們僅舉帶一個參數的介面作為例子。

7、QwtTextEngineDict類:文本引擎字典類,用於根據文字格式設定查詢文本引擎。

class QwtTextEngineDict{public:    static QwtTextEngineDict &dict(); // 單例模式    void setTextEngine( QwtText::TextFormat, QwtTextEngine * );    const QwtTextEngine *textEngine( QwtText::TextFormat ) const;    const QwtTextEngine *textEngine( const QString &,        QwtText::TextFormat ) const;private:    QwtTextEngineDict(); // 建構函式,聲明為私人的    ~QwtTextEngineDict();    typedef QMap<int, QwtTextEngine *> EngineMap;     inline const QwtTextEngine *engine( EngineMap::const_iterator &it ) const // 將it.value();封裝為一個函數,一為了方便;二使代碼可讀    {        return it.value();    }    EngineMap d_map; // 原廠模式,在建構函式中即被初始化};QwtTextEngineDict &QwtTextEngineDict::dict(){    static QwtTextEngineDict engineDict; //  單例模式,對象只會被建立一次    return engineDict; // 返回對象的引用}QwtTextEngineDict::QwtTextEngineDict(){    d_map.insert( QwtText::PlainText, new QwtPlainTextEngine() );#ifndef QT_NO_RICHTEXT    d_map.insert( QwtText::RichText, new QwtRichTextEngine() );#endif}QwtTextEngineDict::~QwtTextEngineDict() // 解構函式{    for ( EngineMap::const_iterator it = d_map.begin();        it != d_map.end(); ++it )    {        const QwtTextEngine *textEngine = engine( it );        delete textEngine;    }}

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

QwtTextLabel類: 一個用於顯示QwtText的表單組件(QWidget),繼承自QFrame。QwtTextLabel類的實現不難,是一個很好的教學樣本,告訴我們如何自訂自己的控制項。

主要通過

程式碼分析:

1、設定縮排:

/*!  Set label's text indent in pixels  \param indent Indentation in pixels*/void QwtTextLabel::setIndent( int indent ){    if ( indent < 0 ) // 防錯性設計,增強健壯性        indent = 0;    d_data->indent = indent;    update();    updateGeometry();}

2、重新實現“表單組件大小”的介面:

    virtual QSize sizeHint() const;    virtual QSize minimumSizeHint() const;    virtual int heightForWidth( int ) const;

3、繪製表單形狀和樣式:

protected:    virtual void paintEvent( QPaintEvent *e );    virtual void drawContents( QPainter * );    virtual void drawText( QPainter *, const QRect & );

--------------------------------------
QwtLegendItem類繼承自QwtTextLabel,表徵QwtLegend列表組件中的一個子項目,其包含一個樣本圖QPixmap(被稱作identifier)和一個文本名稱QwtText。QwtLegendItem具有唯讀(ReadOnlyItem),可點擊(ClickableItem)和可選中(CheckableItem)等3種狀態。除了重新實現了繪製事件函數外,還重新實現了滑鼠事件函數。

1、屬性資料:

class QwtLegendItem::PrivateData{public:    PrivateData():        itemMode( QwtLegend::ReadOnlyItem ),        isDown( false ),        identifierSize( 8, 8 ),        spacing( Margin )    {    }    QwtLegend::LegendItemMode itemMode;    bool isDown;    QSize identifierSize; // 圖示大小    QPixmap identifier; // 圖示    int spacing;};

2、重新實現的虛函數:

    virtual void paintEvent( QPaintEvent * );    virtual void mousePressEvent( QMouseEvent * );    virtual void mouseReleaseEvent( QMouseEvent * );    virtual void keyPressEvent( QKeyEvent * );    virtual void keyReleaseEvent( QKeyEvent * );

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.