File indexing completed on 2024-03-03 15:49:08

0001 /***************************************************************************
0002     smb4ktooltip  -  Provides tooltips for Smb4K
0003                              -------------------
0004     begin                : Sa Dez 23 2010
0005     copyright            : (C) 2010-2017 by Alexander Reinholdt
0006     email                : alexander.reinholdt@kdemail.net
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *   This program is free software; you can redistribute it and/or modify  *
0011  *   it under the terms of the GNU General Public License as published by  *
0012  *   the Free Software Foundation; either version 2 of the License, or     *
0013  *   (at your option) any later version.                                   *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful, but   *
0016  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
0018  *   General Public License for more details.                              *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program; if not, write to the                         *
0022  *   Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,*
0023  *   MA 02110-1335, USA                                                    *
0024  ***************************************************************************/
0025 
0026 #ifdef HAVE_CONFIG_H
0027 #include <config.h>
0028 #endif
0029 
0030 // application specific includes
0031 #include "smb4ktooltip.h"
0032 #include "core/smb4kbasicnetworkitem.h"
0033 #include "core/smb4kworkgroup.h"
0034 #include "core/smb4khost.h"
0035 #include "core/smb4kshare.h"
0036 #include "core/smb4kglobal.h"
0037 
0038 // Qt includes
0039 #include <QTimer>
0040 #include <QDebug>
0041 #include <QPalette>
0042 #include <QLabel>
0043 #include <QToolTip>
0044 #include <QApplication>
0045 #include <QDesktopWidget>
0046 #include <QPainterPath>
0047 #include <QStylePainter>
0048 #include <QStyle>
0049 #include <QStyleOptionFrame>
0050 #include <QAbstractScrollArea>
0051 #include <QScreen>
0052 
0053 // KDE includes
0054 #include <KI18n/KLocalizedString>
0055 #include <KIconThemes/KIconLoader>
0056 #include <KWidgetsAddons/KSeparator>
0057 #include <KConfigWidgets/KColorScheme>
0058 #include <KWindowSystem/KWindowSystem>
0059 
0060 using namespace Smb4KGlobal;
0061 
0062 
0063 Smb4KToolTip::Smb4KToolTip(QWidget* parent)
0064 : QWidget(parent, Qt::ToolTip|Qt::BypassGraphicsProxyWidget|Qt::FramelessWindowHint),
0065   m_item(NetworkItemPtr()), m_tip_layout(0), m_info_layout(0), m_text_layout(0)
0066 {
0067   qDebug() << "Use KToolTipWidget for the tooltips";
0068   
0069   m_master_browser_label = 0;
0070   m_comment_label        = 0;
0071   m_ip_label             = 0;
0072   m_mounted_label        = 0;
0073   m_size_label           = 0;
0074   
0075   setAttribute(Qt::WA_TranslucentBackground);
0076   
0077   // Copied from QToolTip
0078   setForegroundRole(QPalette::ToolTipText);
0079   setBackgroundRole(QPalette::ToolTipBase);
0080   setPalette(QToolTip::palette());
0081   ensurePolished();
0082   setWindowOpacity(style()->styleHint(QStyle::SH_ToolTipLabel_Opacity, 0, this) / 255.0);
0083 } 
0084 
0085 
0086 Smb4KToolTip::~Smb4KToolTip()
0087 {
0088   // Never delete m_item here. We only have a pointer to 
0089   // somewhere outside of this class.
0090 }
0091 
0092 
0093 void Smb4KToolTip::setup(Smb4KToolTip::Parent parent, const NetworkItemPtr &item)
0094 {
0095   if (item)
0096   {
0097     m_item = item;
0098   
0099     // Set up tool tip.
0100     switch (parent)
0101     {
0102       case NetworkBrowser:
0103       {
0104         setupNetworkBrowserToolTip();
0105         break;
0106       }
0107       case SharesView:
0108       {
0109         setupSharesViewToolTip();
0110         break;
0111       }
0112       default:
0113       {
0114         return;
0115       }
0116     }
0117   }
0118 }
0119 
0120 
0121 void Smb4KToolTip::update(Smb4KToolTip::Parent parent, const NetworkItemPtr &item)
0122 {
0123   if (item)
0124   {
0125     m_item = item;
0126     
0127     switch (parent)
0128     {
0129       case NetworkBrowser:
0130       {
0131         switch (m_item->type())
0132         {
0133           case Workgroup:
0134           {
0135             WorkgroupPtr workgroup = item.staticCast<Smb4KWorkgroup>();
0136             
0137             if (workgroup->hasMasterBrowserIpAddress())
0138             {
0139               m_master_browser_label->setText(workgroup->masterBrowserName()+" ("+workgroup->masterBrowserIpAddress()+')');
0140             }
0141             else
0142             {
0143               m_master_browser_label->setText(workgroup->masterBrowserName());
0144             }
0145             break;
0146           }
0147           case Host:
0148           {
0149             HostPtr host = item.staticCast<Smb4KHost>();
0150             
0151             if (!host->comment().isEmpty())
0152             {
0153               m_comment_label->setText(host->comment());
0154             }
0155             else
0156             {
0157               m_comment_label->setText("-");
0158             }
0159             
0160             if (host->hasIpAddress())
0161             {
0162               m_ip_label->setText(host->ipAddress());
0163             }
0164             else
0165             {
0166               m_ip_label->setText("-");
0167             }
0168             break;
0169           }
0170           case Share:
0171           {
0172             SharePtr share = item.staticCast<Smb4KShare>();
0173             
0174             if (!share->comment().isEmpty())
0175             {
0176               m_comment_label->setText(share->comment());
0177             }
0178             else
0179             {
0180               m_comment_label->setText("-");
0181             }
0182             
0183             if (!share->isPrinter())
0184             {
0185               if (share->isMounted())
0186               {
0187                 m_mounted_label->setText(i18n("yes"));
0188               }
0189               else
0190               {
0191                 m_mounted_label->setText(i18n("no"));
0192               }
0193             }
0194             else
0195             {
0196               m_mounted_label->setText("-");
0197             }
0198             
0199             if (share->hasHostIpAddress())
0200             {
0201               m_ip_label->setText(share->hostIpAddress());
0202             }
0203             else
0204             {
0205               m_ip_label->setText("-");
0206             }
0207             
0208             break;
0209           }
0210           default:
0211           {
0212             break;
0213           }
0214         }
0215         break;
0216       }
0217       case SharesView:
0218       {
0219         SharePtr share = item.staticCast<Smb4KShare>();
0220         
0221         if (share->totalDiskSpace() != 0 && share->freeDiskSpace() != 0)
0222         {
0223           m_size_label->setText(i18n("%1 of %2 free (%3 used)",
0224                                  share->freeDiskSpaceString(),
0225                                  share->totalDiskSpaceString(),
0226                                  share->diskUsageString()));
0227         }
0228         else
0229         {
0230           m_size_label->setText(i18n("unknown"));
0231         }
0232         break;
0233       }
0234       default:
0235       {
0236         break;
0237       }
0238     }    
0239   }
0240 }
0241 
0242  
0243 void Smb4KToolTip::show(const QPoint &pos)
0244 {
0245   // Get the geometry of the screen where the cursor is
0246 #if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
0247   const QRect screenRect = QApplication::desktop()->screenGeometry(pos);
0248 #else
0249   const QRect screenRect = QApplication::screenAt(pos)->geometry();
0250 #endif
0251   
0252   // Adjust the size
0253   adjustSize();
0254 
0255   // The position where the tooltip is to be shown
0256   QPoint tooltipPos;  
0257   
0258   // Correct the position of the tooltip, so that it is completely 
0259   // shown.
0260   if (pos.x() + width() + 5 >= screenRect.x() + screenRect.width())
0261   {
0262     tooltipPos.setX(pos.x() - width() - 5);
0263   }
0264   else
0265   {
0266     tooltipPos.setX(pos.x() + 5);
0267   }
0268   
0269   if (pos.y() + height() + 5 >= screenRect.y() + screenRect.height())
0270   {
0271     tooltipPos.setY(pos.y() - height() - 5);
0272   }
0273   else
0274   {
0275     tooltipPos.setY(pos.y() + 5);
0276   }
0277 
0278   move(tooltipPos);
0279   setVisible(true);
0280   
0281   QTimer::singleShot(10000, this, SLOT(slotHideToolTip()));
0282 }
0283  
0284  
0285 void Smb4KToolTip::hide()
0286 {
0287   setVisible(false);
0288 }
0289 
0290 
0291 void Smb4KToolTip::setupNetworkBrowserToolTip()
0292 {
0293   // NOTE: If you change the layout here, adjust also the update function!
0294   
0295   m_tip_layout = new QHBoxLayout(this);
0296   m_tip_layout->setAlignment(Qt::AlignTop);
0297   m_info_layout = new QVBoxLayout();
0298   m_info_layout->setAlignment(Qt::AlignTop);
0299 
0300   // Set the icon
0301   QLabel *icon_label = new QLabel(this);
0302   icon_label->setPixmap(m_item->icon().pixmap(KIconLoader::SizeEnormous));
0303   
0304   m_tip_layout->addWidget(icon_label, Qt::AlignHCenter);
0305   m_tip_layout->addLayout(m_info_layout);
0306   
0307   // Use a brighter color for the left label. This was copied from
0308   // KFileMetaDataWidget class.
0309   QPalette p = palette();
0310   const QPalette::ColorRole role = foregroundRole();
0311   QColor textColor = p.color(role);
0312   textColor.setAlpha(128);
0313   p.setColor(role, textColor);
0314   
0315   // FIXME: Use smaller font for the information. Get the current 
0316   // point size of the window system with QFontInfo::pointSize().
0317   
0318   switch (m_item->type())
0319   {
0320     case Workgroup:
0321     {
0322       WorkgroupPtr workgroup = m_item.staticCast<Smb4KWorkgroup>();
0323       
0324       QLabel *caption = new QLabel(workgroup->workgroupName(), this);
0325       caption->setAlignment(Qt::AlignHCenter);
0326       QFont caption_font = caption->font();
0327       caption_font.setBold(true);
0328       caption->setFont(caption_font);
0329       
0330       m_info_layout->addWidget(caption);
0331       m_info_layout->addWidget(new KSeparator(this), Qt::AlignHCenter);
0332       
0333       m_text_layout = new QGridLayout();
0334       
0335       QLabel *type_label = new QLabel(i18n("Type"), this);
0336       type_label->setPalette(p);
0337       
0338       m_text_layout->addWidget(type_label, 0, 0, Qt::AlignRight);
0339       m_text_layout->addWidget(new QLabel(i18n("Workgroup"), this), 0, 1, 0);
0340       
0341       QLabel *mb_label = new QLabel(i18n("Master browser"), this);
0342       mb_label->setPalette(p);
0343       
0344       m_text_layout->addWidget(mb_label, 1, 0, Qt::AlignRight);
0345       
0346       if (workgroup->hasMasterBrowserIpAddress())
0347       {
0348         m_master_browser_label = new QLabel(workgroup->masterBrowserName()+" ("+workgroup->masterBrowserIpAddress()+')', this);
0349         m_text_layout->addWidget(m_master_browser_label, 1, 1, 0);
0350       }
0351       else
0352       {
0353         m_master_browser_label = new QLabel(workgroup->masterBrowserName(), this);
0354         m_text_layout->addWidget(m_master_browser_label, 1, 1, 0);
0355       }
0356       
0357       m_info_layout->addLayout(m_text_layout);
0358       m_info_layout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));
0359       break;
0360     }
0361     case Host:
0362     {
0363       HostPtr host = m_item.staticCast<Smb4KHost>();
0364       
0365       QLabel *caption = new QLabel(host->hostName(), this);
0366       caption->setAlignment(Qt::AlignHCenter);
0367       QFont caption_font = caption->font();
0368       caption_font.setBold(true);
0369       caption->setFont(caption_font);
0370       
0371       m_info_layout->addWidget(caption, Qt::AlignHCenter);
0372       m_info_layout->addWidget(new KSeparator(this), Qt::AlignHCenter);
0373 
0374       m_text_layout = new QGridLayout();
0375       
0376       QLabel *type_label = new QLabel(i18n("Type"), this);
0377       type_label->setPalette(p);
0378       
0379       m_text_layout->addWidget(type_label, 0, 0, Qt::AlignRight);
0380       m_text_layout->addWidget(new QLabel(i18n("Host"), this), 0, 1, 0);
0381       
0382       QLabel *co_label = new QLabel(i18n("Comment"), this);
0383       co_label->setPalette(p);
0384       
0385       m_text_layout->addWidget(co_label, 1, 0, Qt::AlignRight);
0386       
0387       if (!host->comment().isEmpty())
0388       {
0389         m_comment_label = new QLabel(host->comment(), this);
0390         m_text_layout->addWidget(m_comment_label, 1, 1, 0);
0391       }
0392       else
0393       {
0394         m_comment_label = new QLabel("-", this);
0395         m_text_layout->addWidget(m_comment_label, 1, 1, 0);
0396       }
0397       
0398       QLabel *ip_label = new QLabel(i18n("IP Address"), this);
0399       ip_label->setPalette(p);
0400       
0401       m_text_layout->addWidget(ip_label, 2, 0, Qt::AlignRight);
0402       
0403       if (host->hasIpAddress())
0404       {
0405         m_ip_label = new QLabel(host->ipAddress(), this);
0406         m_text_layout->addWidget(m_ip_label, 2, 1, 0);
0407       }
0408       else
0409       {
0410         m_ip_label = new QLabel("-", this);
0411         m_text_layout->addWidget(m_ip_label, 2, 1, 0);
0412       }
0413       
0414       QLabel *wg_label = new QLabel(i18n("Workgroup"), this);
0415       wg_label->setPalette(p);
0416       
0417       m_text_layout->addWidget(wg_label, 3, 0, Qt::AlignRight);
0418       m_text_layout->addWidget(new QLabel(host->workgroupName(), this), 3, 1, 0);
0419       
0420       m_info_layout->addLayout(m_text_layout);
0421       m_info_layout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));
0422       break;
0423     }
0424     case Share:
0425     {
0426       SharePtr share = m_item.staticCast<Smb4KShare>();
0427       
0428       QLabel *caption = new QLabel(share->shareName(), this);
0429       caption->setAlignment(Qt::AlignHCenter);
0430       QFont caption_font = caption->font();
0431       caption_font.setBold(true);
0432       caption->setFont(caption_font);
0433       
0434       m_info_layout->addWidget(caption);
0435       m_info_layout->addWidget(new KSeparator(this), Qt::AlignHCenter);
0436       
0437       m_text_layout = new QGridLayout();
0438       
0439       QLabel *type_label = new QLabel(i18n("Type"), this);
0440       type_label->setPalette(p);
0441       
0442       m_text_layout->addWidget(type_label, 0, 0, Qt::AlignRight);
0443       m_text_layout->addWidget(new QLabel(i18n("Share (%1)", share->shareTypeString()), this), 0, 1, 0);
0444       
0445       QLabel *co_label = new QLabel(i18n("Comment"), this);
0446       co_label->setPalette(p);
0447       
0448       m_text_layout->addWidget(co_label, 1, 0, Qt::AlignRight);
0449       
0450       if (!share->comment().isEmpty())
0451       {
0452         m_comment_label = new QLabel(share->comment(), this);
0453         m_text_layout->addWidget(m_comment_label, 1, 1, 0);
0454       }
0455       else
0456       {
0457         m_comment_label = new QLabel("-", this);
0458         m_text_layout->addWidget(m_comment_label, 1, 1, 0);
0459       }
0460      
0461       QLabel *mnt_label = new QLabel(i18n("Mounted"), this);
0462       mnt_label->setPalette(p);
0463       
0464       m_text_layout->addWidget(mnt_label, 2, 0, Qt::AlignRight);
0465       
0466       if (!share->isPrinter())
0467       {
0468         if (share->isMounted())
0469         {
0470           m_mounted_label = new QLabel(i18n("yes"), this);
0471           m_text_layout->addWidget(m_mounted_label, 2, 1, 0);
0472         }
0473         else
0474         {
0475           m_mounted_label = new QLabel(i18n("no"), this);
0476           m_text_layout->addWidget(m_mounted_label, 2, 1, 0);
0477         }
0478       }
0479       else
0480       {
0481         m_mounted_label = new QLabel("-", this);
0482         m_text_layout->addWidget(m_mounted_label, 2, 1, 0);
0483       }
0484       
0485       QLabel *h_label = new QLabel(i18n("Host"), this);
0486       h_label->setPalette(p);
0487       
0488       m_text_layout->addWidget(h_label, 3, 0, Qt::AlignRight);
0489       m_text_layout->addWidget(new QLabel(share->hostName()), 3, 1, 0);
0490 
0491       QLabel *ip_label = new QLabel(i18n("IP Address"), this);
0492       ip_label->setPalette(p);
0493       
0494       m_text_layout->addWidget(ip_label, 4, 0, Qt::AlignRight);
0495       
0496       if (share->hasHostIpAddress())
0497       {
0498         m_ip_label = new QLabel(share->hostIpAddress(), this);
0499         m_text_layout->addWidget(m_ip_label, 4, 1, 0);
0500       }
0501       else
0502       {
0503         m_ip_label = new QLabel("-", this);
0504         m_text_layout->addWidget(m_ip_label, 4, 1, 0);
0505       }
0506       
0507       QLabel *unc_label = new QLabel(i18n("Location"), this);
0508       unc_label->setPalette(p);
0509       
0510       m_text_layout->addWidget(unc_label, 5, 0, Qt::AlignRight);
0511       m_text_layout->addWidget(new QLabel(share->displayString(), this), 5, 1, 0);
0512       
0513       m_info_layout->addLayout(m_text_layout);
0514       m_info_layout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));
0515       break;
0516     }
0517     default:
0518     {
0519       return;
0520     }
0521   }
0522   
0523   adjustSize();
0524 }
0525 
0526 
0527 void Smb4KToolTip::setupSharesViewToolTip()
0528 {
0529   // NOTE: If you change the layout here, adjust also the update function!
0530   
0531   SharePtr share = m_item.staticCast<Smb4KShare>();
0532   
0533   m_tip_layout = new QHBoxLayout(this);
0534   m_tip_layout->setAlignment(Qt::AlignTop);
0535   m_info_layout = new QVBoxLayout();
0536   m_info_layout->setAlignment(Qt::AlignTop);
0537 
0538   // Set the icon
0539   QLabel *icon_label = new QLabel(this);
0540   icon_label->setPixmap(share->icon().pixmap(KIconLoader::SizeEnormous));
0541   
0542   m_tip_layout->addWidget(icon_label, Qt::AlignHCenter);
0543   m_tip_layout->addLayout(m_info_layout);
0544   
0545   // Use a brighter color for the left label. This was copied from
0546   // KFileMetaDataWidget class.
0547   QPalette p = palette();
0548   const QPalette::ColorRole role = foregroundRole();
0549   QColor textColor = p.color(role);
0550   textColor.setAlpha(128);
0551   p.setColor(role, textColor);
0552   
0553   // FIXME: Use smaller font for the information. Get the current 
0554   // point size of the window system with QFontInfo::pointSize().
0555       
0556   QLabel *caption = new QLabel(share->shareName(), this);
0557   caption->setAlignment(Qt::AlignHCenter);
0558   QFont caption_font = caption->font();
0559   caption_font.setBold(true);
0560   caption->setFont(caption_font);
0561       
0562   m_info_layout->addWidget(caption);
0563   m_info_layout->addWidget(new KSeparator(this), Qt::AlignHCenter);
0564       
0565   m_text_layout = new QGridLayout();
0566       
0567   QLabel *unc_label = new QLabel(i18n("Location"), this);
0568   unc_label->setPalette(p);
0569       
0570   m_text_layout->addWidget(unc_label, 0, 0, Qt::AlignRight);
0571   m_text_layout->addWidget(new QLabel(share->displayString(), this), 0, 1, 0);
0572       
0573   QLabel *mp_label = new QLabel(i18n("Mountpoint"), this);
0574   mp_label->setPalette(p);
0575       
0576   m_text_layout->addWidget(mp_label, 1, 0, Qt::AlignRight);
0577   m_text_layout->addWidget(new QLabel(share->path(), this), 1, 1, 0);
0578 
0579   QLabel *log_label = new QLabel(i18n("Login"), this);
0580   log_label->setPalette(p);
0581       
0582   m_text_layout->addWidget(log_label, 2, 0, Qt::AlignRight);
0583       
0584   if (!share->login().isEmpty())
0585   {
0586     m_text_layout->addWidget(new QLabel(share->login(), this), 2, 1, 0);
0587   }
0588   else
0589   {
0590     m_text_layout->addWidget(new QLabel(i18n("unknown"), this), 2, 1, 0);
0591   }
0592   
0593   QLabel *own_label = new QLabel(i18n("Owner"), this);
0594   own_label->setPalette(p);
0595       
0596   m_text_layout->addWidget(own_label, 3, 0, Qt::AlignRight);
0597   
0598   QString owner = (!share->user().loginName().isEmpty() ? share->user().loginName() : i18n("unknown"));
0599   QString group = (!share->group().name().isEmpty() ? share->group().name() : i18n("unknown"));
0600   
0601   m_text_layout->addWidget(new QLabel(QString("%1 - %2")
0602                             .arg(owner).arg(group), this), 3, 1, 0);
0603       
0604   QLabel *fs_label = new QLabel(i18n("File system"), this);
0605   fs_label->setPalette(p);
0606   
0607   m_text_layout->addWidget(fs_label, 4, 0, Qt::AlignRight);
0608   m_text_layout->addWidget(new QLabel(share->fileSystemString()), 4, 1, 0);
0609   
0610   QLabel *s_label = new QLabel(i18n("Size"), this);
0611   s_label->setPalette(p);
0612   
0613   m_text_layout->addWidget(s_label, 5, 0, Qt::AlignRight);
0614   
0615   if (share->totalDiskSpace() != 0 && share->freeDiskSpace() != 0)
0616   {
0617     m_size_label = new QLabel(i18n("%1 free of %2 (%3 used)",
0618                                share->freeDiskSpaceString(),
0619                                share->totalDiskSpaceString(),
0620                                share->diskUsageString()));
0621     m_text_layout->addWidget(m_size_label, 5, 1, 0);
0622   }
0623   else
0624   {
0625     m_size_label = new QLabel(i18n("unknown"));
0626     m_text_layout->addWidget(m_size_label, 5, 1, 0);
0627   }
0628       
0629   m_info_layout->addLayout(m_text_layout);
0630   m_info_layout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));
0631 }
0632 
0633 
0634 void Smb4KToolTip::updateNetworkBrowserToolTip()
0635 {
0636   if (m_item && m_text_layout && m_tip_layout)
0637   {
0638     switch (m_item->type())
0639     {
0640       case Workgroup:
0641       {
0642         WorkgroupPtr workgroup = m_item.staticCast<Smb4KWorkgroup>();
0643         
0644         // Master browser name and IP address
0645         QLayoutItem *mb_item = m_text_layout->itemAtPosition(1, 1);
0646         QLabel *mb_label = static_cast<QLabel *>(mb_item->widget());
0647         
0648         if (mb_label)
0649         {
0650           if (!workgroup->hasMasterBrowserIpAddress())
0651           {
0652             mb_label->setText(workgroup->masterBrowserName()+" ("+workgroup->masterBrowserIpAddress()+')');
0653           }
0654           else
0655           {
0656             mb_label->setText(workgroup->masterBrowserName());
0657           }
0658         }
0659         
0660         break;
0661       }
0662       case Host:
0663       {
0664         HostPtr host = m_item.staticCast<Smb4KHost>();
0665 
0666         // Comment
0667         QLayoutItem *co_item = m_text_layout->itemAtPosition(1, 1);
0668         QLabel *co_label = static_cast<QLabel *>(co_item->widget());
0669         
0670         if (co_label)
0671         {
0672           if (!host->comment().isEmpty())
0673           {
0674             co_label->setText(host->comment());
0675           }
0676           else
0677           {
0678             co_label->setText("-");
0679           }
0680         }
0681       
0682         // IP address
0683         QLayoutItem *ip_item = m_text_layout->itemAtPosition(2, 1);
0684         QLabel *ip_label = static_cast<QLabel *>(ip_item->widget());
0685         
0686         if (ip_label)
0687         {
0688           if (host->hasIpAddress())
0689           {
0690             ip_label->setText(host->ipAddress());
0691           }
0692           else
0693           {
0694             ip_label->setText("-");
0695           }
0696         }
0697       
0698         break;
0699       }
0700       case Share:
0701       {
0702         SharePtr share = m_item.staticCast<Smb4KShare>();
0703         
0704         // Icon
0705         QLayoutItem *icon_item = m_tip_layout->itemAt(0);
0706         QLabel *icon_label = static_cast<QLabel *>(icon_item->widget());
0707         icon_label->setPixmap(m_item->icon().pixmap(KIconLoader::SizeEnormous));
0708 
0709         // Comment
0710         QLayoutItem *co_item = m_text_layout->itemAtPosition(1, 1);
0711         QLabel *co_label = static_cast<QLabel *>(co_item->widget());
0712 
0713         if (co_label)
0714         {
0715           if (!share->comment().isEmpty())
0716           {
0717             co_label->setText(share->comment());
0718           }
0719           else
0720           {
0721             co_label->setText("-");
0722           }
0723         }
0724      
0725         // Mounted indicator
0726         QLayoutItem *mnt_item = m_text_layout->itemAtPosition(2, 1);
0727         QLabel *mnt_label = static_cast<QLabel *>(mnt_item->widget());
0728         
0729         if (mnt_label)
0730         {
0731           if (!share->isPrinter())
0732           {
0733             if (share->isMounted())
0734             {
0735               mnt_label->setText(i18n("yes"));
0736             }
0737             else
0738             {
0739               mnt_label->setText(i18n("no"));
0740             }
0741           }
0742           else
0743           {
0744             mnt_label->setText("-");
0745           }
0746         }
0747         
0748         // The rest won't change while the tool tip is shown.
0749       
0750         break;
0751       }
0752       default:
0753       {
0754         break;
0755       }
0756     }
0757   }
0758 }
0759 
0760 
0761 void Smb4KToolTip::updateSharesViewToolTip()
0762 {
0763   if (m_item && m_text_layout && m_tip_layout)
0764   {
0765     SharePtr share = m_item.staticCast<Smb4KShare>();
0766     
0767     // Set the icon
0768     QLayoutItem *icon_item = m_tip_layout->itemAt(0);
0769     QLabel *icon_label = static_cast<QLabel *>(icon_item->widget());
0770     icon_label->setPixmap(m_item->icon().pixmap(KIconLoader::SizeEnormous));
0771     
0772     QLayoutItem *log_item = m_text_layout->itemAtPosition(2, 1);
0773     QLabel *log_label = static_cast<QLabel *>(log_item->widget());
0774     
0775     if (!share->login().isEmpty())
0776     {
0777       log_label->setText(share->login());
0778     }
0779     else
0780     {
0781       log_label->setText(i18n("unknown"));
0782     }
0783     
0784     QLayoutItem *s_item = m_text_layout->itemAtPosition(5, 1);
0785     QLabel *s_label = static_cast<QLabel *>(s_item->widget());
0786   
0787     if (share->totalDiskSpace() != 0 && share->freeDiskSpace() != 0)
0788     {
0789       s_label->setText(i18n("%1 free of %2 (%3 used)", 
0790                               share->freeDiskSpaceString(),
0791                               share->totalDiskSpaceString(),
0792                               share->diskUsageString()));
0793     }
0794     else
0795     {
0796       s_label->setText(i18n("unknown"));
0797     }
0798   }
0799   
0800   // The rest won't change while the tool tip is shown.
0801 }
0802 
0803 
0804 void Smb4KToolTip::paintEvent(QPaintEvent *e)
0805 {
0806   // Copied from Dolphin's meta data tool tips.
0807   Q_UNUSED(e);
0808 
0809   QPainter painter(this);
0810 
0811   QColor toColor = palette().brush(QPalette::ToolTipBase).color();
0812   QColor fromColor = KColorScheme::shade(toColor, KColorScheme::LightShade, 0.2);
0813 
0814   const bool haveAlphaChannel = KWindowSystem::compositingActive();
0815   
0816   if (haveAlphaChannel)
0817   {
0818     painter.setRenderHint(QPainter::Antialiasing);
0819     painter.translate(0.5, 0.5);
0820     toColor.setAlpha(220);
0821     fromColor.setAlpha(220);
0822   }
0823 
0824   QLinearGradient gradient(QPointF(0.0, 0.0), QPointF(0.0, height()));
0825   gradient.setColorAt(0.0, fromColor);
0826   gradient.setColorAt(1.0, toColor);
0827   painter.setPen(Qt::NoPen);
0828   painter.setBrush(gradient);
0829 
0830   const QRect rect(0, 0, width(), height());
0831     
0832   if (haveAlphaChannel) 
0833   {
0834     const qreal radius = 5.0;
0835 
0836     QPainterPath path;
0837     path.moveTo(rect.left(), rect.top() + radius);
0838     arc(path, rect.left() + radius, rect.top() + radius, radius, 180, -90);
0839     arc(path, rect.right() - radius, rect.top() + radius, radius, 90, -90);
0840     arc(path, rect.right() - radius, rect.bottom() - radius, radius, 0, -90);
0841     arc(path, rect.left() + radius, rect.bottom() - radius, radius, 270, -90);
0842     path.closeSubpath();
0843 
0844     painter.drawPath(path);
0845   } 
0846   else 
0847   {
0848     painter.drawRect(rect);
0849   }
0850 }
0851 
0852 
0853 void Smb4KToolTip::arc(QPainterPath& path,
0854                               qreal cx, qreal cy,
0855                               qreal radius, qreal angle,
0856                               qreal sweepLength)
0857 {
0858   // Copied from Dolphin's meta data tool tips.
0859   path.arcTo(cx-radius, cy-radius, radius * 2, radius * 2, angle, sweepLength);
0860 }
0861 
0862 
0863 void Smb4KToolTip::slotHideToolTip()
0864 {
0865   hide();
0866 }
0867