summaryrefslogtreecommitdiffstats
path: root/sidebar/linkview.cpp
blob: b012b20b28dbf9e2240aa73cb8c44e1bd6be623d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

#include <stdlib.h>
#include <tqcursor.h>
#include <tqsplitter.h>
#include <tdelocale.h>
#include <tqdir.h>
#include <tdeconfig.h>
#include <tqtimer.h>
#include "dndlistbox.h"
#include "listboxlink.h"
#include "linkview.h"
#include "linkview.moc"

// internal class to eat invalid leave envents (i.e. leave that does not leave the rect but just enters the splitter, as styles (like baghira ;) may post install eventfilters that'd cause useless repaints and therefore flicker if the scroller appereance changes ;)
class EventKiller : public TQObject
{
protected:
   virtual bool eventFilter( TQObject *o, TQEvent *e)
   {
      if (e->type() == TQEvent::Leave)
         return ((TQScrollView*)o)->rect().contains(((TQScrollView*)o)->mapFromGlobal (TQCursor::pos()));
      return false;
   }
};

LinkView::LinkView(TQWidget * parent, const char * name, WFlags f):
                   TQScrollView(parent, name, f)
{
   setFrameShape( TQFrame::StyledPanel );
   setFrameShadow( TQFrame::Sunken );
   setBackgroundMode(TQt::PaletteBase);
   _blocked = FALSE;
   splitter = new TQSplitter( TQt::Vertical, viewport() );
   addChild(splitter);
   splitter->setMargin(5);
   splitter->setBackgroundMode(TQt::PaletteBase);
   splitter->setSizePolicy(TQSizePolicy::Expanding,TQSizePolicy::Minimum);
   splitter->setFrameShape( TQFrame::NoFrame );
   splitter->setChildrenCollapsible(TRUE);
   splitter->setHandleWidth( 3 );
   splitter->setOpaqueResize();
   hardware = new MediaListBox(splitter, "hardware");
   splitter->setResizeMode( hardware, TQSplitter::KeepSize );
   hardware->setFrameShape( TQFrame::NoFrame );
   hardware->setHScrollBarMode(TQScrollView::AlwaysOff);
   hardware->setVScrollBarMode(TQScrollView::AlwaysOff);
   locations = new DnDListBox(splitter, "locations");
//    splitter->setResizeMode( locations, TQSplitter::KeepSize );
   locations->setFrameShape( TQFrame::NoFrame );
   locations->setHScrollBarMode(TQScrollView::AlwaysOff);
   locations->setVScrollBarMode(TQScrollView::AlwaysOff);
   // custom area, locations
   loadLinks();
   locations->setCurrentItem(0);
   locations->setSelected( locations->selectedItem(), false );
   hardware->installEventFilter(this);
   connect (hardware, TQ_SIGNAL(highlighted( int )), this, TQ_SLOT(unselectLocations()));
   connect (locations, TQ_SIGNAL(highlighted( int )), this, TQ_SLOT(unselectHardware()));
   connect (hardware, TQ_SIGNAL(scrolled(int,int)), this, TQ_SLOT(scrollBy(int,int)));
   connect (locations, TQ_SIGNAL(scrolled(int,int)), this, TQ_SLOT(scrollBy(int,int)));
   connect (hardware, TQ_SIGNAL(itemNumberChanged(bool)), this, TQ_SLOT(adjustSplitter2Hardware(bool)));
   connect (locations, TQ_SIGNAL(itemNumberChanged(bool)), this, TQ_SLOT(adjustSplitter2Locations()));
   TQTimer::singleShot(50, this, TQ_SLOT(adjustSplitter2Locations()));
   TQTimer::singleShot(60, this, TQ_SLOT(postInstallEventFilter()));
}

static EventKiller *eventKiller = 0L;

LinkView::~LinkView()
{
   
   saveLinks();
   delete eventKiller; eventKiller = 0L;
}

void LinkView::postInstallEventFilter()
{
   eventKiller = new EventKiller;
   installEventFilter(eventKiller);
}

bool LinkView::eventFilter(TQObject *o, TQEvent *e)
{
   if (o != hardware)
      return TQScrollView::eventFilter(o, e);
   if (_blocked || e->type() != TQEvent::Resize)
      return FALSE; // not a resize - non of our business
   TQResizeEvent *rev = (TQResizeEvent*)e;
   if (rev->size().height() == rev->oldSize().height())
      return FALSE; // height didn't change
   int tmpH = rev->size().height() + locations->numRows()*locations->itemHeight()+20;
   if (tmpH < viewport()->height())
      tmpH = viewport()->height();
   if (tmpH != splitter->height())
   {
      _blocked = TRUE;
      splitter->resize ( splitter->width(), tmpH );
      _blocked = FALSE;
   }

   return FALSE;
}

void LinkView::adjustSplitter2Locations()
{
   int tmpH = hardware->height() + locations->numRows()*locations->itemHeight()+20;
   if (tmpH < viewport()->height())
      tmpH = viewport()->height();
   if (tmpH != splitter->height())
      splitter->resize ( viewport()->width(), tmpH );
}

void LinkView::adjustSplitter2Hardware(bool added)
{
   if (added)
   {
      if (hardware->height() < hardware->numRows()*hardware->itemHeight())
         hardware->resize ( hardware->width(), hardware->numRows()*hardware->itemHeight() );
   }
   else
      if (hardware->height() > hardware->numRows()*hardware->itemHeight())
         hardware->resize ( hardware->width(), hardware->numRows()*hardware->itemHeight() );
}

void LinkView::viewportResizeEvent( TQResizeEvent *rev )
{
   int tmpH = hardware->height() + locations->numRows()*locations->itemHeight()+20;
   if (tmpH < rev->size().height())
      tmpH = rev->size().height();
   splitter->resize(rev->size().width(), tmpH);
}

void LinkView::unselectLocations()
{
   if (locations) locations->setSelected( locations->selectedItem(), FALSE );
}

void LinkView::unselectHardware()
{
   if (hardware) hardware->setSelected( hardware->selectedItem(), FALSE );
}

void LinkView::loadLinks()
{
   if (!locations)
      return;
   TDEConfig config(TQDir::homeDirPath() + "/.qt/baghirarc");
   config.setGroup("Sidebar");
   splitter->setSizes(config.readIntListEntry ("Sizes"));
   loadedLinks = (uint)config.readNumEntry("NumLinks", 0);
   locations->blockSignals ( true );
   if (loadedLinks == 0) // no settings stored - load defaults
   {
      locations->insertItem("desktop", i18n("Desktop"), TQDir::homeDirPath()+"/Desktop");
      locations->insertItem("folder_home", getenv("USER"), TQDir::homeDirPath());
      locations->insertItem("kmenu", i18n("Programs"), "programs:/");
   }
   TQString num;
   for (uint i = 0; i < loadedLinks; i++)
   {
      TQString title;
      TQString icon;
      TQString url;
      num.setNum(i);
      title = config.readEntry("Link_"+num+"_Title", "???");
      icon = config.readEntry("Link_"+num+"_Icon", "empty");
      url = config.readEntry("Link_"+num+"_URL", TQDir::homeDirPath());
      locations->insertItem(icon, title, url);
   }
   locations->blockSignals ( false );
}

void LinkView::saveLinks()
{
   if (!locations)
      return;
   TDEConfig *config = new TDEConfig(TQDir::homeDirPath() + "/.qt/baghirarc");
   config->setGroup("Sidebar");
   config->writeEntry("Sizes", splitter->sizes());
   config->writeEntry("NumLinks", (int)locations->count());
   TQString num;
   for (uint i = 0; i < locations->count(); i++)
   {
      num.setNum(i);
      ListBoxLink *current = (ListBoxLink*)locations->item(i);
      config->writeEntry("Link_"+num+"_Title", current->text());
      config->writeEntry("Link_"+num+"_Icon", current->icon());
      config->writeEntry("Link_"+num+"_URL", current->URL());
   }
   // reduced links, remove them from settings
   for (uint i = locations->count(); i < loadedLinks; i++)
   {
      num.setNum(i);
      config->deleteEntry("Link_"+num+"_Title");
      config->deleteEntry("Link_"+num+"_Icon");
      config->deleteEntry("Link_"+num+"_URL");
   }
   delete config;
}