Plasma
tooltipcontent.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright 2008 by Aaron Seigo <aseigo@kde.org> 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Lesser General Public 00006 * License as published by the Free Software Foundation; either 00007 * version 2.1 of the License, or (at your option) any later version. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Lesser General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public 00015 * License along with this library; if not, write to the Free Software 00016 * Foundation, Inc., 51 Franklin St, Fifth Floor, 00017 * Boston, MA 02110-1301 USA 00018 */ 00019 00020 #include "tooltipcontent.h" 00021 00022 #include <QGraphicsWidget> 00023 #include <QHash> 00024 #include <QTextDocument> 00025 00026 #include <kiconloader.h> 00027 00028 namespace Plasma 00029 { 00030 00031 struct ToolTipResource 00032 { 00033 ToolTipResource() 00034 { 00035 } 00036 00037 ToolTipResource(ToolTipContent::ResourceType t, const QVariant &v) 00038 : type(t), 00039 data(v) 00040 { 00041 } 00042 00043 ToolTipContent::ResourceType type; 00044 QVariant data; 00045 }; 00046 00047 const int MAXIMUM_TEXT_LENGTH = 5000; 00048 00049 class ToolTipContentPrivate 00050 { 00051 public: 00052 ToolTipContentPrivate() 00053 : autohide(true), 00054 clickable(false), 00055 highlightWindows(false) 00056 { 00057 } 00058 00059 QString mainText; 00060 QString subText; 00061 QPixmap image; 00062 QList<WId> windowsToPreview; 00063 QHash<QString, ToolTipResource> resources; 00064 QWeakPointer<QGraphicsWidget> graphicsWidget; 00065 bool autohide : 1; 00066 bool clickable : 1; 00067 bool highlightWindows : 1; 00068 }; 00069 00070 ToolTipContent::ToolTipContent() 00071 : d(new ToolTipContentPrivate) 00072 { 00073 } 00074 00075 ToolTipContent::ToolTipContent(const ToolTipContent &other) 00076 : d(new ToolTipContentPrivate(*other.d)) 00077 { 00078 } 00079 00080 ToolTipContent::~ToolTipContent() 00081 { 00082 delete d; 00083 } 00084 00085 ToolTipContent &ToolTipContent::operator=(const ToolTipContent &other) 00086 { 00087 *d = *other.d; 00088 return *this; 00089 } 00090 00091 ToolTipContent::ToolTipContent(const QString &mainText, 00092 const QString &subText, 00093 const QPixmap &image) 00094 : d(new ToolTipContentPrivate) 00095 { 00096 d->mainText = mainText; 00097 d->subText = subText; 00098 d->image = image; 00099 } 00100 00101 ToolTipContent::ToolTipContent(const QString &mainText, 00102 const QString &subText, 00103 const QIcon &icon) 00104 : d(new ToolTipContentPrivate) 00105 { 00106 d->mainText = mainText; 00107 d->subText = subText; 00108 d->image = icon.pixmap(IconSize(KIconLoader::Desktop)); 00109 } 00110 00111 bool ToolTipContent::isEmpty() const 00112 { 00113 return d->mainText.isEmpty() && 00114 d->subText.isEmpty() && 00115 d->image.isNull() && 00116 (d->windowsToPreview.size() == 0); 00117 } 00118 00119 void ToolTipContent::setMainText(const QString &text) 00120 { 00121 d->mainText = text; 00122 } 00123 00124 QString ToolTipContent::mainText() const 00125 { 00126 QString text = d->mainText; 00127 text.truncate(MAXIMUM_TEXT_LENGTH); 00128 return text; 00129 } 00130 00131 void ToolTipContent::setSubText(const QString &text) 00132 { 00133 d->subText = text; 00134 } 00135 00136 QString ToolTipContent::subText() const 00137 { 00138 QString text = d->subText; 00139 text.truncate(MAXIMUM_TEXT_LENGTH); 00140 return text; 00141 } 00142 00143 void ToolTipContent::setImage(const QPixmap &image) 00144 { 00145 d->image = image; 00146 } 00147 00148 void ToolTipContent::setImage(const QIcon &icon) 00149 { 00150 d->image = icon.pixmap(IconSize(KIconLoader::Desktop)); 00151 } 00152 00153 QPixmap ToolTipContent::image() const 00154 { 00155 return d->image; 00156 } 00157 00158 void ToolTipContent::setWindowToPreview(WId id) 00159 { 00160 d->windowsToPreview.clear(); 00161 d->windowsToPreview.append(id); 00162 } 00163 00164 WId ToolTipContent::windowToPreview() const 00165 { 00166 if (d->windowsToPreview.size() == 1) { 00167 return d->windowsToPreview.first(); 00168 } else { 00169 return 0; 00170 } 00171 } 00172 00173 void ToolTipContent::setWindowsToPreview(const QList<WId> & ids) 00174 { 00175 d->windowsToPreview = ids; 00176 } 00177 00178 QList<WId> ToolTipContent::windowsToPreview() const 00179 { 00180 return d->windowsToPreview; 00181 } 00182 00183 void ToolTipContent::setHighlightWindows(bool highlight) 00184 { 00185 d->highlightWindows = highlight; 00186 } 00187 00188 bool ToolTipContent::highlightWindows() const 00189 { 00190 return d->highlightWindows; 00191 } 00192 00193 void ToolTipContent::setAutohide(bool autohide) 00194 { 00195 d->autohide = autohide; 00196 } 00197 00198 bool ToolTipContent::autohide() const 00199 { 00200 return d->autohide; 00201 } 00202 00203 void ToolTipContent::addResource(ResourceType type, const QUrl &path, const QVariant &resource) 00204 { 00205 d->resources.insert(path.toString(), ToolTipResource(type, resource)); 00206 } 00207 00208 void ToolTipContent::registerResources(QTextDocument *document) const 00209 { 00210 if (!document) { 00211 return; 00212 } 00213 00214 QHashIterator<QString, ToolTipResource> it(d->resources); 00215 while (it.hasNext()) { 00216 it.next(); 00217 const ToolTipResource &r = it.value(); 00218 QTextDocument::ResourceType t; 00219 00220 switch (r.type) { 00221 case ImageResource: 00222 t = QTextDocument::ImageResource; 00223 break; 00224 case HtmlResource: 00225 t = QTextDocument::HtmlResource; 00226 break; 00227 case CssResource: 00228 t = QTextDocument::StyleSheetResource; 00229 break; 00230 } 00231 00232 document->addResource(t, it.key(), r.data); 00233 } 00234 } 00235 00236 void ToolTipContent::setClickable(bool clickable) 00237 { 00238 d->clickable = clickable; 00239 } 00240 00241 bool ToolTipContent::isClickable() const 00242 { 00243 return d->clickable; 00244 } 00245 00246 void ToolTipContent::setGraphicsWidget(QGraphicsWidget *widget) 00247 { 00248 d->graphicsWidget = widget; 00249 } 00250 00251 QGraphicsWidget *ToolTipContent::graphicsWidget() const 00252 { 00253 return d->graphicsWidget.data(); 00254 } 00255 00256 } // namespace Plasma 00257 00258
KDE 4.6 API Reference