Peter Kümmel wrote:
Peter Kümmel wrote:

This is the Qt message:
QPixmap::operator=: Cannot assign to pixmap during painting
[...]
The assignment fails because on the old QPixmap painting is still active.

But why is painting sometime active? After hours of debugging I've found
the reason.

[...]

But in QLPainter -hidden by a nested call- this code is
effectively executed,

QPainter qp(qwa_->paintDevice());
QPainter qp2(qwa_->paintDevice());

which contradicts

"p->begin( myWidget );
 p2->begin( myWidget ); // impossible - only one painter at a time"

of the doc. And there is NO warning from Qt!

So, this is the patch which fixes the resize bug:

Very good Peter! So we don't need the shared_ptr in WorkArea then?

And here are some cleanups:

- setQPainterPen needs not to return its parameter QPainter&

Good, note that in my QPainter cache try, I transformed this setQPainterPen into a "QPainter * getQPainter(...)". Please find attached an outdated patch (that won't apply) if you want to continue this work.

- split painting commands into two lines

Agreed.

- color is 'col'
- use operator[]

OK.

Please commit to "trunk" and... to "younes" if you want be kind with me ;-)

Abdel.
Index: QLPainter.C
===================================================================
--- QLPainter.C (revision 14065)
+++ QLPainter.C (working copy)
@@ -41,8 +41,13 @@
 }
 
 QLPainter::QLPainter(WorkArea * qwa)
-       : Painter(), qwa_(qwa)
+       : Painter(), qwa_(qwa), current_color_(LColor::ignore),
+       current_qp_(NULL)
 {
+       for (int i=0; i < LColor::ignore; ++i)
+               for (int j = 0; j < 2; ++j)
+                       for (int k = 0; k < 2; ++k)
+                               qpainters_[i][j][k] = 0;
 }
 
 
@@ -57,18 +62,27 @@
        return qwa_->viewport()->height();
 }
 
-QPainter & QLPainter::setQPainterPen(QPainter & qp, LColor_color c,
+QPainter * QLPainter::getQPainter(LColor_color c,
        Painter::line_style ls, Painter::line_width lw)
 {
-       if (c == current_color_ && ls == current_ls_ && lw == current_lw_)
-               return qp;
+       if (c == current_color_ && ls == current_ls_ && lw == current_lw_
+               && current_qp_)
+               return current_qp_;
 
        current_color_ = c;
        current_ls_ = ls;
        current_lw_ = lw;
 
-       QPen pen = qp.pen();
+       // fi is a reference to the pointer type (QLFontInfo *) in the
+       // fontinfo_ table.
+       QPainter * & qp =
+               qpainters_[current_color_][current_ls_][current_lw_];
+       if (qp)
+               return qp;
 
+       qp = new QPainter;
+
+       QPen pen = qp->pen();
        pen.setColor(lcolorcache.get(c));
 
        switch (ls) {
@@ -81,15 +95,18 @@
                case line_thick: pen.setWidth(3); break;
        }
 
-       qp.setPen(pen);
+       qp->setPen(pen);
+       current_qp_ = qp;
 
        return qp;
 }
 
 void QLPainter::point(int x, int y, LColor_color c)
 {
-       QPainter qp(qwa_->paintDevice());
-       setQPainterPen(qp, c).drawPoint(x, y);
+       QPainter * qp = getQPainter(c);
+       qp->begin(qwa_->paintDevice());
+       qp->drawPoint(x, y);
+       qp->end();
 }
 
 
@@ -98,8 +115,10 @@
        line_style ls,
        line_width lw)
 {
-       QPainter qp(qwa_->paintDevice());
-       setQPainterPen(qp, col, ls, lw).drawLine(x1, y1, x2, y2);
+       QPainter * qp = getQPainter(col, ls, lw);
+       qp->begin(qwa_->paintDevice());
+       qp->drawLine(x1, y1, x2, y2);
+       qp->end();
 }
 
 
@@ -118,8 +137,10 @@
                points[i].setY(yp[i]);
        }
 
-       QPainter qp(qwa_->paintDevice());
-       setQPainterPen(qp, col, ls, lw).drawPolyline(points.get(), np);
+       QPainter * qp = getQPainter(col, ls, lw);
+       qp->begin(qwa_->paintDevice());
+       qp->drawPolyline(points.get(), np);
+       qp->end();
 }
 
 
@@ -128,8 +149,10 @@
        line_style ls,
        line_width lw)
 {
-       QPainter qp(qwa_->paintDevice());
-       setQPainterPen(qp, col, ls, lw).drawRect(x, y, w, h);
+       QPainter * qp = getQPainter(col, ls, lw);
+       qp->begin(qwa_->paintDevice());
+       qp->drawRect(x, y, w, h);
+       qp->end();
 }
 
 
@@ -143,19 +166,6 @@
 void QLPainter::fillPolygon(int const * xp, int const * yp,
        int np, LColor_color col)
 {
-       // Must use new as np is not known at compile time.
-       boost::scoped_array<QPoint> points(new QPoint[np]);
-
-       for (int i = 0; i < np; ++i) {
-               points[i].setX(xp[i]);
-               points[i].setY(yp[i]);
-       }
-
-       QPainter qp(qwa_->paintDevice());
-       setQPainterPen(qp, col);
-       qp.setBrush(lcolorcache.get(col));
-       qp.drawPolygon(points.get(), np);
-       qp.setBrush(Qt::NoBrush);
 }
 
 
@@ -163,8 +173,10 @@
        int a1, int a2, LColor_color col)
 {
        // LyX usings 1/64ths degree, Qt usings 1/16th
-       QPainter qp(qwa_->paintDevice());
-       setQPainterPen(qp, col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
+       QPainter * qp = getQPainter(col);
+       qp->begin(qwa_->paintDevice());
+       qp->drawArc(x, y, w, h, a1 / 4, a2 / 4);
+       qp->end();
 }
 
 
@@ -227,9 +239,6 @@
 void QLPainter::text(int x, int y, char const * s, size_t ls,
        LyXFont const & f)
 {
-       QPainter qp(qwa_->paintDevice());
-       setQPainterPen(qp, f.realColor());
-
        Encoding const * encoding = f.language()->encoding();
        if (f.isSymbolFont())
                encoding = encodings.symbol_encoding();
@@ -244,11 +253,14 @@
                str = ' ' + str;
 
        if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
-               qp.setFont(fontloader.get(f));
+               QPainter * qp = getQPainter(f.realColor());
+               qp->begin(qwa_->paintDevice());
+               qp->setFont(fontloader.get(f));
                // We need to draw the text as LTR as we use our own bidi
                // code.
-               qp.setLayoutDirection(Qt::LeftToRight);
-               qp.drawText(x, y, str);
+               qp->setLayoutDirection(Qt::LeftToRight);
+               qp->drawText(x, y, str);
+               qp->end();
        } else {
                smallCapsText(x, y, str, f);
        }
@@ -256,7 +268,6 @@
        if (f.underbar() == LyXFont::ON) {
                underline(f, x, y, font_metrics::width(s, ls, f));
        }
-
 }
 /// draw a pixmap from the image cache
 void QLPainter::drawPixmap(int x, int y, QPixmap const & pixmap)
Index: QLPainter.h
===================================================================
--- QLPainter.h (revision 14065)
+++ QLPainter.h (working copy)
@@ -142,19 +142,24 @@
                QString const & str, LyXFont const & f);
 
        /// set pen parameters
-       QPainter & setQPainterPen(QPainter & qp, LColor_color c,
+       QPainter * getQPainter(LColor_color c,
                line_style ls = line_solid,
                line_width lw = line_thin);
 
        /// our qt painter
        boost::scoped_ptr<QPainter> qp_;
 
+
        /// the working area
        WorkArea * qwa_;
 
        LColor::color current_color_;
        Painter::line_style current_ls_;
        Painter::line_width current_lw_;
+       QPainter * current_qp_;
+
+       /// BUTT ugly !
+       QPainter * qpainters_[LColor::ignore][2][2];
 };
 
 } // namespace frontend

Reply via email to