File indexing completed on 2024-10-06 04:35:39

0001 /*
0002     smb4knetworkbrowser  -  The network browser widget of Smb4K.
0003 
0004     SPDX-FileCopyrightText: 2007-2023 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 // application specific includes
0009 #include "smb4knetworkbrowser.h"
0010 #include "core/smb4kglobal.h"
0011 #include "core/smb4ksettings.h"
0012 #include "core/smb4kshare.h"
0013 #include "smb4knetworkbrowseritem.h"
0014 #include "smb4ktooltip.h"
0015 
0016 // Qt includes
0017 #include <QApplication>
0018 #include <QHeaderView>
0019 #include <QMouseEvent>
0020 #include <QTimer>
0021 
0022 #include <QLayout>
0023 
0024 // KDE includes
0025 #include <KLocalizedString>
0026 
0027 using namespace Smb4KGlobal;
0028 
0029 Smb4KNetworkBrowser::Smb4KNetworkBrowser(QWidget *parent)
0030     : QTreeWidget(parent)
0031 {
0032     setRootIsDecorated(true);
0033     setAllColumnsShowFocus(false);
0034     setMouseTracking(true);
0035     setSelectionMode(ExtendedSelection);
0036 
0037     setContextMenuPolicy(Qt::CustomContextMenu);
0038 
0039     m_toolTip = new Smb4KToolTip(this);
0040 
0041     QStringList header_labels;
0042     header_labels.append(i18n("Network"));
0043     header_labels.append(i18n("Type"));
0044     header_labels.append(i18n("IP Address"));
0045     header_labels.append(i18n("Comment"));
0046     setHeaderLabels(header_labels);
0047 
0048     header()->setSectionResizeMode(QHeaderView::ResizeToContents);
0049 
0050     //
0051     // Connections
0052     //
0053     connect(this, &Smb4KNetworkBrowser::itemActivated, this, &Smb4KNetworkBrowser::slotItemActivated);
0054     connect(this, &Smb4KNetworkBrowser::itemSelectionChanged, this, &Smb4KNetworkBrowser::slotItemSelectionChanged);
0055 }
0056 
0057 Smb4KNetworkBrowser::~Smb4KNetworkBrowser()
0058 {
0059 }
0060 
0061 Smb4KToolTip *Smb4KNetworkBrowser::toolTip()
0062 {
0063     return m_toolTip;
0064 }
0065 
0066 bool Smb4KNetworkBrowser::event(QEvent *e)
0067 {
0068     switch (e->type()) {
0069     case QEvent::ToolTip: {
0070         QPoint pos = viewport()->mapFromGlobal(cursor().pos());
0071         Smb4KNetworkBrowserItem *item = static_cast<Smb4KNetworkBrowserItem *>(itemAt(pos));
0072 
0073         if (item) {
0074             if (Smb4KSettings::showNetworkItemToolTip()) {
0075                 int ind = 0;
0076 
0077                 switch (item->type()) {
0078                 case Host: {
0079                     ind = 2;
0080                     break;
0081                 }
0082                 case Share: {
0083                     ind = 3;
0084                     break;
0085                 }
0086                 default: {
0087                     ind = 1;
0088                     break;
0089                 }
0090                 }
0091 
0092                 if (pos.x() > ind * indentation()) {
0093                     m_toolTip->setupToolTip(Smb4KToolTip::NetworkItem, item->networkItem());
0094                     m_toolTip->show(cursor().pos(), nativeParentWidget()->windowHandle());
0095                 }
0096             }
0097         }
0098 
0099         break;
0100     }
0101     default: {
0102         break;
0103     }
0104     }
0105 
0106     return QTreeWidget::event(e);
0107 }
0108 
0109 void Smb4KNetworkBrowser::mousePressEvent(QMouseEvent *e)
0110 {
0111     if (m_toolTip->isVisible()) {
0112         m_toolTip->hide();
0113     }
0114 
0115     QTreeWidgetItem *item = itemAt(e->pos());
0116 
0117     if (!item && currentItem()) {
0118         currentItem()->setSelected(false);
0119         setCurrentItem(nullptr);
0120     }
0121 
0122     QTreeWidget::mousePressEvent(e);
0123 }
0124 
0125 void Smb4KNetworkBrowser::mouseMoveEvent(QMouseEvent *e)
0126 {
0127     if (m_toolTip->isVisible()) {
0128         m_toolTip->hide();
0129     }
0130 
0131     QTreeWidget::mouseMoveEvent(e);
0132 }
0133 
0134 /////////////////////////////////////////////////////////////////////////////
0135 // SLOT IMPLEMENTATIONS
0136 /////////////////////////////////////////////////////////////////////////////
0137 
0138 void Smb4KNetworkBrowser::slotItemActivated(QTreeWidgetItem *item, int /*column*/)
0139 {
0140     // Only do something if there are no keyboard modifiers pressed
0141     // and there is only one item selected.
0142     if (QApplication::keyboardModifiers() == Qt::NoModifier && selectedItems().size() == 1) {
0143         if (item) {
0144             switch (item->type()) {
0145             case Workgroup:
0146             case Host: {
0147                 if (!item->isExpanded()) {
0148                     expandItem(item);
0149                 } else {
0150                     collapseItem(item);
0151                 }
0152 
0153                 break;
0154             }
0155             default: {
0156                 break;
0157             }
0158             }
0159         }
0160     }
0161 }
0162 
0163 void Smb4KNetworkBrowser::slotItemSelectionChanged()
0164 {
0165     if (selectedItems().size() > 1) {
0166         // If multiple items are selected, only allow shares
0167         // to stay selected.
0168         for (int i = 0; i < selectedItems().size(); ++i) {
0169             Smb4KNetworkBrowserItem *item = static_cast<Smb4KNetworkBrowserItem *>(selectedItems()[i]);
0170 
0171             if (item) {
0172                 switch (item->networkItem()->type()) {
0173                 case Workgroup:
0174                 case Host: {
0175                     item->setSelected(false);
0176                     break;
0177                 }
0178                 case Share: {
0179                     if (item->shareItem()->isPrinter()) {
0180                         item->setSelected(false);
0181                     }
0182                     break;
0183                 }
0184                 default: {
0185                     break;
0186                 }
0187                 }
0188             }
0189         }
0190     }
0191 }