Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, June 2007
Qt 4.3.0 PATCH: implement QLabel::setSearchPaths()


Message 1 in thread

Almost one and a half years ago it has been suggested that
QLabel should have a search path for <img> tags, just as
QTextBrowser does. This task has been scheduled, rescheduled,
supposedly fixed, closed, reactivated - and now it's "pending"
again.

I'm now desperate enough to try patching Qt 4.3.0 myself
to hack in this functionality. The attached patch works
for me, maybe it's also useful for others. It uses the
hook Qt internally uses for Qt3 compatibility, but since I
have deactivated Qt3 compatibility I thought it might be
feasible to do it this way.

A real Qt-style solution might look different, but I'm afraid
I can't wait until this happens (if ever)...

Klaus Schmidinger
# allow setting search paths for QLabels (TT#102293):
# allow setting search paths for QLabels (TT#102293):
--- src/gui/widgets/qlabel.cpp.001	2007-05-25 15:30:21.000000000 +0200
+++ src/gui/widgets/qlabel.cpp	2007-06-11 10:18:39.252083178 +0200
@@ -24,6 +24,7 @@
 #include "qdrawutil.h"
 #include "qapplication.h"
 #include "qabstractbutton.h"
+#include "qfileinfo.h"
 #include "qstyle.h"
 #include "qstyleoption.h"
 #include <limits.h>
@@ -33,6 +34,25 @@
 #include <qurl.h>
 #include "qlabel_p.h"
 
+#include "private/qtextimagehandler_p.h"
+static QStringList Search_Paths;
+
+static QImage QLabelRichTextImageLoader(const QString &name, const QString &context)
+{
+    QString fileName;
+    if (QFileInfo(name).isAbsolute())
+        return QImage(name);
+
+    foreach (QString path, Search_Paths) {
+        if (!path.endsWith(QLatin1Char('/')))
+            path.append(QLatin1Char('/'));
+        path.append(name);
+        if (QFileInfo(path).isReadable())
+            return QImage(path);
+    }
+    return QImage();
+}
+
 /*!
     \class QLabel
     \brief The QLabel widget provides a text or image display.
@@ -285,6 +305,18 @@
 }
 
 
+QStringList QLabel::searchPaths() const
+{
+    Q_D(const QLabel);
+    return d->searchPaths();
+}
+
+void QLabel::setSearchPaths(const QStringList &paths)
+{
+    Q_D(QLabel);
+    d->setSearchPaths(paths);
+}
+
 /*!
     \property QLabel::text
     \brief the label's text
@@ -1407,8 +1439,12 @@
     if (control) {
         QTextDocument *doc = control->document();
         if (textDirty) {
-            if (isRichText)
+            if (isRichText) {
+                Search_Paths = search_Paths;
+                QTextImageHandler::externalLoader = QLabelRichTextImageLoader;
                 doc->setHtml(text);
+                QTextImageHandler::externalLoader = NULL;
+            }
             else
                 doc->setPlainText(text);
             doc->setUndoRedoEnabled(false);
# allow setting search paths for QLabels (TT#102293):
# allow setting search paths for QLabels (TT#102293):
--- src/gui/widgets/qlabel.h.001	2007-05-25 15:30:21.000000000 +0200
+++ src/gui/widgets/qlabel.h	2007-06-11 10:21:41.897047086 +0200
@@ -58,6 +58,12 @@
     QMovie *movie() const;
 #endif
 
+#define QT_PATCH_CS20070611_1
+//XXX allow setting search paths for QLabels (TT#102293):
+    QStringList searchPaths() const;
+    void setSearchPaths(const QStringList &paths);
+//XXX
+
     Qt::TextFormat textFormat() const;
     void setTextFormat(Qt::TextFormat);
 
# allow setting search paths for QLabels (TT#102293):
# allow setting search paths for QLabels (TT#102293):
--- src/gui/widgets/qlabel_p.h.001	2007-05-25 15:30:21.000000000 +0200
+++ src/gui/widgets/qlabel_p.h	2007-06-11 10:02:43.680735060 +0200
@@ -63,6 +63,7 @@
     mutable QSizePolicy sizePolicy;
     int margin;
     QString text;
+    QStringList search_Paths;
     QPixmap  *pixmap;
     QPixmap *scaledpixmap;
     QImage *cachedimage;
@@ -100,6 +101,9 @@
                    || (!isRichText && (textInteractionFlags & (Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard))));
     }
 
+    QStringList searchPaths() const { return search_Paths; }
+    void setSearchPaths(const QStringList &paths) { search_Paths = paths; }
+
     void ensureTextPopulated() const;
     void ensureTextLayouted() const;
     void ensureTextControl() const;