~ubuntu-branches/ubuntu/utopic/smb4k/utopic-proposed

« back to all changes in this revision

Viewing changes to smb4k/browser/smb4knetworkbrowser.cpp

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-05-19 18:54:34 UTC
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: package-import@ubuntu.com-20120519185434-kpqpdrg5i47zm7tl
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
    smb4knetworkbrowser  -  The network browser widget of Smb4K.
3
3
                             -------------------
4
4
    begin                : Mo Jan 8 2007
5
 
    copyright            : (C) 2007 by Alexander Reinholdt
6
 
    email                : dustpuppy@users.berlios.de
 
5
    copyright            : (C) 2007-2011 by Alexander Reinholdt
 
6
    email                : alexander.reinholdt@kdemail.net
7
7
 ***************************************************************************/
8
8
 
9
9
/***************************************************************************
19
19
 *                                                                         *
20
20
 *   You should have received a copy of the GNU General Public License     *
21
21
 *   along with this program; if not, write to the                         *
22
 
 *   Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,   *
23
 
 *   MA  02111-1307 USA                                                    *
 
22
 *   Free Software Foundation, 51 Franklin Street, Suite 500, Boston,      *
 
23
 *   MA 02110-1335, USA                                                    *
24
24
 ***************************************************************************/
25
25
 
26
26
// Qt includes
37
37
#include <klocale.h>
38
38
#include <kglobalsettings.h>
39
39
#include <kapplication.h>
 
40
#include <kiconloader.h>
40
41
 
41
42
// application specific includes
42
43
#include <smb4knetworkbrowser.h>
43
44
#include <smb4knetworkbrowseritem.h>
44
 
#include <smb4knetworkbrowsertooltip.h>
 
45
#include <../tooltips/smb4ktooltip.h>
45
46
#include <core/smb4ksettings.h>
46
47
 
47
48
 
52
53
  setAllColumnsShowFocus( false );
53
54
  setMouseTracking( true );
54
55
  setSelectionMode( SingleSelection );
55
 
 
 
56
  
56
57
  setContextMenuPolicy( Qt::CustomContextMenu );
57
58
 
58
 
  m_tooltip = new Smb4KNetworkBrowserToolTip( this );
 
59
  m_tooltip = new Smb4KToolTip( this );
59
60
  m_mouse_inside = false;
60
 
  m_tooltip_timer = new QTimer( this );
61
61
  m_auto_select_timer = new QTimer( this );
62
62
 
63
63
  QStringList header_labels;
70
70
  header()->setResizeMode( QHeaderView::ResizeToContents );
71
71
 
72
72
  // Add some connections:
73
 
  connect( this, SIGNAL( itemExpanded( QTreeWidgetItem * ) ),
74
 
           this, SLOT( slotItemExpanded( QTreeWidgetItem * ) ) );
75
 
 
76
 
  connect( this, SIGNAL( itemCollapsed( QTreeWidgetItem * ) ),
77
 
           this, SLOT( slotItemCollapsed( QTreeWidgetItem * ) ) );
78
 
 
79
73
  connect( this, SIGNAL( itemExecuted( QTreeWidgetItem *, int ) ),
80
74
           this, SLOT( slotItemExecuted( QTreeWidgetItem *, int ) ) );
81
75
 
102
96
}
103
97
 
104
98
 
 
99
bool Smb4KNetworkBrowser::event( QEvent *e )
 
100
{
 
101
  switch ( e->type() )
 
102
  {
 
103
    case QEvent::ToolTip:
 
104
    {
 
105
      // Intercept the tool tip event and show our own tool tip.
 
106
      QPoint pos = viewport()->mapFromGlobal( cursor().pos() );
 
107
      Smb4KNetworkBrowserItem *item = static_cast<Smb4KNetworkBrowserItem *>( itemAt( pos ) );
 
108
      
 
109
      if ( item )
 
110
      {
 
111
        if ( Smb4KSettings::showNetworkItemToolTip() )
 
112
        {
 
113
          int ind = 0;
 
114
 
 
115
          switch ( item->type() )
 
116
          {
 
117
            case Smb4KNetworkBrowserItem::Host:
 
118
            {
 
119
              ind = 2;
 
120
              break;
 
121
            }
 
122
            case Smb4KNetworkBrowserItem::Share:
 
123
            {
 
124
              ind = 3;
 
125
              break;
 
126
            }
 
127
            default:
 
128
            {
 
129
              ind = 1;
 
130
              break;
 
131
            }
 
132
          }
 
133
          
 
134
          // Check that the tooltip is not over the root decoration.
 
135
          // If it is, hide it.
 
136
          if ( pos.x() <= ind * indentation() )
 
137
          {
 
138
            if ( m_tooltip->isVisible() )
 
139
            {
 
140
              m_tooltip->hide();
 
141
            }
 
142
            else
 
143
            {
 
144
              // Do nothing
 
145
            }
 
146
          }
 
147
          else
 
148
          {
 
149
            if ( !m_tooltip->isVisible() || (m_tooltip->networkItem() && 
 
150
                QString::compare( item->networkItem()->key(), m_tooltip->networkItem()->key() ) != 0) )
 
151
            {
 
152
              switch ( item->type() )
 
153
              {
 
154
                case Smb4KNetworkBrowserItem::Workgroup:
 
155
                {
 
156
                  m_tooltip->show( item->workgroupItem(), pos );
 
157
                  break;
 
158
                }
 
159
                case Smb4KNetworkBrowserItem::Host:
 
160
                {
 
161
                  m_tooltip->show( item->hostItem(), pos );
 
162
                  break;
 
163
                }
 
164
                case Smb4KNetworkBrowserItem::Share:
 
165
                {
 
166
                  m_tooltip->show( item->shareItem(), pos );
 
167
                  break;
 
168
                }
 
169
                default:
 
170
                {
 
171
                  break;
 
172
                }
 
173
              }
 
174
            }
 
175
            else
 
176
            {
 
177
              // Do nothing
 
178
            }
 
179
          }
 
180
        }
 
181
        else
 
182
        {
 
183
          if ( m_tooltip->isVisible() )
 
184
          {
 
185
            m_tooltip->hide();
 
186
          }
 
187
          else
 
188
          {
 
189
            // Do nothing
 
190
          }
 
191
        }
 
192
      }
 
193
      else
 
194
      {
 
195
        if ( m_tooltip->isVisible() )
 
196
        {
 
197
          m_tooltip->hide();
 
198
        }
 
199
        else
 
200
        {
 
201
          // Do nothing
 
202
        }
 
203
      }
 
204
      
 
205
      break;
 
206
    }
 
207
    default:
 
208
    {
 
209
      break;
 
210
    }
 
211
  }
 
212
  
 
213
  return QTreeWidget::event( e );
 
214
}
 
215
 
 
216
 
105
217
void Smb4KNetworkBrowser::mouseMoveEvent( QMouseEvent *e )
106
218
{
107
 
  // Store the position of the mouse:
108
 
  m_pos = e->pos();
109
 
 
110
219
  // Find the item over which the user moved the mouse:
111
 
  Smb4KNetworkBrowserItem *item = static_cast<Smb4KNetworkBrowserItem *>( itemAt( m_pos ) );
 
220
  Smb4KNetworkBrowserItem *item = static_cast<Smb4KNetworkBrowserItem *>( itemAt( e->pos() ) );
112
221
 
113
222
  if ( item )
114
223
  {
115
 
    emit itemEntered( item, columnAt( m_pos.x() ) );
 
224
    emit itemEntered( item, columnAt( e->pos().x() ) );
 
225
    
 
226
    // Hide tool tip if the items diverge.
 
227
    if ( m_tooltip->networkItem() && m_tooltip->networkItem() != item->networkItem() )
 
228
    {
 
229
      m_tooltip->hide();
 
230
    }
 
231
    else
 
232
    {
 
233
      // Do nothing
 
234
    }
116
235
  }
117
236
  else
118
237
  {
119
 
    // Do nothing
 
238
    // Hide the tool tip
 
239
    m_tooltip->hide();
120
240
  }
121
241
 
122
242
  QTreeWidget::mouseMoveEvent( e );
125
245
 
126
246
void Smb4KNetworkBrowser::leaveEvent( QEvent *e )
127
247
{
128
 
  slotHideToolTip();
129
 
 
 
248
  if ( m_tooltip->isVisible() )
 
249
  {
 
250
    m_tooltip->hide();
 
251
  }
 
252
  else
 
253
  {
 
254
    // Do nothing
 
255
  }
 
256
  
130
257
  m_auto_select_timer->stop();
131
 
 
132
258
  m_mouse_inside = false;
133
259
 
134
260
  QTreeWidget::leaveEvent( e );
146
272
void Smb4KNetworkBrowser::mousePressEvent( QMouseEvent *e )
147
273
{
148
274
  // Hide the current tool tip so that it is not in the way.
149
 
  slotHideToolTip();
 
275
  if ( m_tooltip->isVisible() )
 
276
  {
 
277
    m_tooltip->hide();
 
278
  }
 
279
  else
 
280
  {
 
281
    // Do nothing
 
282
  }
150
283
 
151
284
  // Get the item that is under the mouse. If there is no
152
285
  // item, unselect the current item.
170
303
void Smb4KNetworkBrowser::focusOutEvent( QFocusEvent *e )
171
304
{
172
305
  m_auto_select_timer->stop();
173
 
 
174
306
  QTreeWidget::focusOutEvent( e );
175
307
}
176
308
 
177
309
 
178
310
void Smb4KNetworkBrowser::wheelEvent( QWheelEvent *e )
179
311
{
180
 
  slotHideToolTip();
181
 
 
 
312
  if ( m_tooltip->isVisible() )
 
313
  {
 
314
    m_tooltip->hide();
 
315
  }
 
316
  else
 
317
  {
 
318
    // Do nothing
 
319
  }
 
320
  
182
321
  QTreeWidget::wheelEvent( e );
183
322
}
184
323
 
187
326
// SLOT IMPLEMENTATIONS
188
327
/////////////////////////////////////////////////////////////////////////////
189
328
 
190
 
void Smb4KNetworkBrowser::slotItemExpanded( QTreeWidgetItem *item )
191
 
{
192
 
  item->setSelected( true );
193
 
}
194
 
 
195
 
 
196
 
void Smb4KNetworkBrowser::slotItemCollapsed( QTreeWidgetItem *item )
197
 
{
198
 
  item->setSelected( false );
199
 
}
200
 
 
201
 
 
202
329
void Smb4KNetworkBrowser::slotItemEntered( QTreeWidgetItem *item, int /*column*/ )
203
330
{
204
331
  // Comply with KDE's settings.
221
348
    // Do nothing
222
349
  }
223
350
 
224
 
  // Now initialize the hiding/showing of the tool tip.
225
 
  Smb4KNetworkBrowserItem *browserItem = static_cast<Smb4KNetworkBrowserItem *>( item );
226
 
 
227
 
  if ( browserItem )
 
351
  if ( m_tooltip->isVisible() )
228
352
  {
229
 
    int indentation_factor = 1;
230
 
 
231
 
    switch ( item->type() )
232
 
    {
233
 
      case Smb4KNetworkBrowserItem::Host:
234
 
      {
235
 
        indentation_factor = 2;
236
 
 
237
 
        break;
238
 
      }
239
 
      case Smb4KNetworkBrowserItem::Share:
240
 
      {
241
 
        indentation_factor = 3;
242
 
 
243
 
        break;
244
 
      }
245
 
      default:
246
 
      {
247
 
        break;
248
 
      }
249
 
    }
250
 
 
251
 
    // Check that the mouse is not over the root decoration.
252
 
    // If it is, hide the tool tip.
253
 
    if ( m_pos.x() <= indentation_factor * indentation() )
254
 
    {
255
 
      slotHideToolTip();
 
353
    Smb4KNetworkBrowserItem *browser_item = static_cast<Smb4KNetworkBrowserItem *>( item );
 
354
  
 
355
    if ( browser_item && m_tooltip->networkItem() &&
 
356
         QString::compare( browser_item->networkItem()->key(), m_tooltip->networkItem()->key() ) != 0 )
 
357
    {
 
358
      m_tooltip->hide();
256
359
    }
257
360
    else
258
361
    {
259
 
      if ( Smb4KSettings::showNetworkItemToolTip() )
260
 
      {
261
 
        if ( m_tooltip->item() == NULL || m_tooltip->item() != browserItem )
262
 
        {
263
 
          if ( !m_tooltip->isVisible() )
264
 
          {
265
 
            m_tooltip->setupToolTip( browserItem );
266
 
 
267
 
            m_tooltip_timer->setSingleShot( true );
268
 
            connect( m_tooltip_timer, SIGNAL( timeout() ), this, SLOT( slotShowToolTip() ) );
269
 
 
270
 
            m_tooltip_timer->start( 2000 );
271
 
          }
272
 
          else
273
 
          {
274
 
            slotHideToolTip();
275
 
          }
276
 
        }
277
 
        else
278
 
        {
279
 
          // Do nothing.
280
 
        }
281
 
      }
282
 
      else
283
 
      {
284
 
        if ( m_tooltip->isVisible() )
285
 
        {
286
 
          slotHideToolTip();
287
 
        }
288
 
        else
289
 
        {
290
 
          // Do nothing
291
 
        }
292
 
      }
 
362
      // Do nothing
293
363
    }
294
364
  }
295
365
  else
313
383
 
314
384
  m_auto_select_timer->stop();
315
385
  m_auto_select_item = 0;
316
 
 
317
 
  // Hide the tool tip.
 
386
  
318
387
  if ( m_tooltip->isVisible() )
319
388
  {
320
 
    slotHideToolTip();
 
389
    m_tooltip->hide();
321
390
  }
322
391
  else
323
392
  {
328
397
 
329
398
void Smb4KNetworkBrowser::slotItemExecuted( QTreeWidgetItem *item, int /*column*/ )
330
399
{
331
 
  if ( !m_tooltip->isCleared() )
 
400
  if ( m_tooltip->isVisible() )
332
401
  {
333
 
    slotHideToolTip();
 
402
    m_tooltip->hide();
334
403
  }
335
404
  else
336
405
  {
368
437
}
369
438
 
370
439
 
371
 
void Smb4KNetworkBrowser::slotShowToolTip()
372
 
{
373
 
  disconnect( m_tooltip_timer, 0, 0, 0 );
374
 
 
375
 
  if ( Smb4KSettings::showNetworkItemToolTip() )
376
 
  {
377
 
    Smb4KNetworkBrowserItem *browserItem = static_cast<Smb4KNetworkBrowserItem *>( itemAt( m_pos ) );
378
 
 
379
 
    if ( browserItem && !m_tooltip->isCleared() )
380
 
    {
381
 
      if ( !m_tooltip->isVisible() )
382
 
      {
383
 
        QPoint p( viewport()->mapToGlobal( m_pos ) );
384
 
 
385
 
        QDesktopWidget *d = QApplication::desktop();
386
 
 
387
 
        if ( p.x() + m_tooltip->width() > d->width() )
388
 
        {
389
 
          p.setX( p.x() - m_tooltip->width() - 5 );
390
 
        }
391
 
        else
392
 
        {
393
 
          p.setX( p.x() + 5 );
394
 
        }
395
 
 
396
 
        if ( p.y() + m_tooltip->height() > d->height() )
397
 
        {
398
 
          p.setY( p.y() - m_tooltip->height() - 5 );
399
 
        }
400
 
        else
401
 
        {
402
 
          p.setY( p.y() + 5 );
403
 
        }
404
 
 
405
 
        m_tooltip->setGeometry( p.x(), p.y(), m_tooltip->width(), m_tooltip->height() );
406
 
        m_tooltip->setVisible( true );
407
 
 
408
 
        m_tooltip_timer->setSingleShot( true );
409
 
        connect( m_tooltip_timer, SIGNAL( timeout() ), this, SLOT( slotHideToolTip() ) );
410
 
 
411
 
        m_tooltip_timer->start( 10000 );
412
 
      }
413
 
      else
414
 
      {
415
 
        // Do nothing
416
 
      }
417
 
    }
418
 
    else
419
 
    {
420
 
      slotHideToolTip();
421
 
    }
422
 
  }
423
 
  else
424
 
  {
425
 
    slotHideToolTip();
426
 
  }
427
 
}
428
 
 
429
 
 
430
 
void Smb4KNetworkBrowser::slotHideToolTip()
431
 
{
432
 
  disconnect( m_tooltip_timer, 0, 0, 0 );
433
 
 
434
 
  m_tooltip->setVisible( false );
435
 
  m_tooltip->clearToolTip();
436
 
}
437
 
 
438
 
 
439
440
void Smb4KNetworkBrowser::slotKDESettingsChanged( int category )
440
441
{
441
442
  // Adjust to KDE's default mouse settings.
476
477
void Smb4KNetworkBrowser::slotAutoSelectItem()
477
478
{
478
479
  // Check that the item is still valid.
479
 
  QTreeWidgetItem *item = itemAt( m_pos );
 
480
  QPoint pos = viewport()->mapFromGlobal( cursor().pos() );
 
481
  QTreeWidgetItem *item = itemAt( pos );
480
482
 
481
483
  if ( !m_auto_select_item || !item || m_auto_select_item != item )
482
484
  {