summaryrefslogtreecommitdiffstats
path: root/quanta/utility/toolbartabwidget.cpp
blob: 873f164c075838e7142659b0f875817773cd8515 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/***************************************************************************
                             toolbartabwidget.cpp
                             ---------------------
    copyright            : (C) 2003 by Andras Mantia <[email protected]>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; version 2 of the License.               *
 *                                                                         *
 ***************************************************************************/

//qt includes
#include <tqevent.h>
#include <tqlayout.h>
#include <tqobjectlist.h>
#include <tqpoint.h>
#include <tqtabbar.h>
#include <tqwidgetstack.h>
#include <tqtabwidget.h>
#include <tqfontmetrics.h>

//kde includes
#include <kaction.h>
#include <kaccelmanager.h>
#include <kdeversion.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kpopupmenu.h>
#include <ktoolbar.h>
#include <ktoolbarbutton.h>
#include <kdebug.h>
#include <kglobalsettings.h>

//app includes
#include "toolbartabwidget.h"

ToolbarTabWidget::ToolbarTabWidget(TQWidget * parent, const char * name, WFlags f)
:TQTabWidget(parent, name, f)
{
  m_popupMenu = new KPopupMenu(this);
  m_popupMenu->insertTitle(i18n("Toolbar Menu"), 1);
  m_popupMenu->insertItem(i18n("New Action..."), parent, TQT_SLOT(slotNewAction()));
  m_popupMenu->insertSeparator();
  m_popupMenu->insertItem(i18n("New Toolbar..."), parent, TQT_SLOT(slotAddToolbar()));
  m_popupMenu->insertItem(i18n("Remove Toolbar"), this, TQT_SLOT(slotRemoveToolbar()));
  m_popupMenu->insertItem(i18n("Rename Toolbar..."), this, TQT_SLOT(slotRenameToolbar()));
  m_popupMenu->insertItem(SmallIconSet("configure_toolbars"), i18n("Configure Toolbars..."), this, TQT_SLOT(slotEditToolbar()));

  connect(this, TQT_SIGNAL(removeToolbar(const TQString&)),
          parent, TQT_SLOT(slotRemoveToolbar(const TQString&)));
  connect(this, TQT_SIGNAL(renameToolbar(const TQString&)),
          parent, TQT_SLOT(slotRenameToolbar(const TQString&)));
  connect(this, TQT_SIGNAL(editToolbar(const TQString&)),
          parent, TQT_SLOT(slotConfigureToolbars(const TQString&)));
  connect(this, TQT_SIGNAL(newAction()),
          parent, TQT_SLOT(slotNewAction()));
  connect(this, TQT_SIGNAL(addToolbar()),
          parent, TQT_SLOT(slotAddToolbar()));
  KAcceleratorManager::setNoAccel(this);
}

void ToolbarTabWidget::insertTab(TQWidget *child, const TQString &label, const TQString &id)
{
  if (child->inherits("KToolBar") && child->parentWidget())
  {
    TQTabWidget::insertTab(child->parentWidget(), label);
    toolbarList.insert(id, child);
  }
}

TQWidget* ToolbarTabWidget::page(int index)
{
  TQWidget *w = TQTabWidget::page(index);

  for (TQMap<TQString, TQWidget*>::Iterator it = toolbarList.begin(); it != toolbarList.end(); ++it)
  {
    if (it.data()->parentWidget() == w)
    {
      w = *it;
      break;
    }
  }
  return w;
}

TQString ToolbarTabWidget::id(TQWidget *w) const
{
  TQString idStr;
  for (TQMap<TQString, TQWidget*>::ConstIterator it = toolbarList.constBegin(); it != toolbarList.constEnd(); ++it)
  {
    if (it.data()->parentWidget() == w)
    {
      idStr = it.key();
      break;
    }
  }
  return idStr;
}

TQString ToolbarTabWidget::id(int index) const
{
  TQWidget *w = TQTabWidget::page(index);
  TQString idStr;
  for (TQMap<TQString, TQWidget*>::ConstIterator it = toolbarList.constBegin(); it != toolbarList.constEnd(); ++it)
  {
    if (it.data()->parentWidget() == w)
    {
      idStr = it.key();
      break;
    }
  }
  return idStr;
}

TQWidget* ToolbarTabWidget::page(const TQString& id)
{
  TQWidget *w = toolbarList.find(id).data();
  return w;
}

void ToolbarTabWidget::removePage(TQWidget * w)
{
  TQWidget *parent = w->parentWidget();
  if (w->inherits("KToolBar") && parent)
  {
    TQTabWidget::removePage(parent);
    for (TQMap<TQString, TQWidget*>::ConstIterator it = toolbarList.constBegin(); it != toolbarList.constEnd(); ++it)
    {
      if (it.data() == w)
      {
        toolbarList.remove(it.key());
        break;
      }
    }
    delete parent;
  }
}

void ToolbarTabWidget::slotRemoveToolbar()
{
  emit removeToolbar(tabUnderMouse.lower());
}

void ToolbarTabWidget::slotRenameToolbar()
{
  emit renameToolbar(tabUnderMouse.lower());
}

void ToolbarTabWidget::slotEditToolbar()
{
  emit editToolbar(tabUnderMouseLabel + " <quanta>");
}

void ToolbarTabWidget::mousePressEvent ( TQMouseEvent * e )
{
  if (e->button() == Qt::RightButton)
  {
    TQPoint p = e->globalPos();
    TQTab *tab = 0L;
    TQWidget *pageW = 0L;
    for (int i =0; i < tabBar()->count(); i++)
    {
      tab = tabBar()->tabAt(i);
      pageW = page(i);
      TQRect r = tab->rect();
      TQPoint p1 = mapToGlobal(r.topLeft());
      TQPoint p2 = mapToGlobal(r.bottomRight());
      if (TQRect(p1, p2).contains(p))
          break;
      else
        tab = 0L;
    }
    tabUnderMouseLabel = tab ? tab->text() : label(currentPageIndex());
    if (!pageW)
      pageW = currentPage();
    for (TQMap<TQString, TQWidget*>::Iterator it = toolbarList.begin(); it != toolbarList.end(); ++it)
    {
      if (it.data()->parentWidget() == pageW)
      {
        tabUnderMouse = it.key();
        break;
      }
    }
    m_popupMenu->changeTitle(1, i18n("Toolbar Menu") + " - " + i18n(tabUnderMouseLabel.utf8()));
    m_popupMenu->popup(p);
  }
}


void ToolbarTabWidget::resizeEvent(TQResizeEvent *e)
{
  TQWidget::resizeEvent(e);
  TQWidget *tb;
  for (TQMap<TQString, TQWidget*>::Iterator it = toolbarList.begin(); it != toolbarList.end(); ++it)
  {
    tb = it.data();
    tb->resize(TQSize(width(), tb->height()));
  }
  int i = currentPageIndex();
  if (i > 0)
  {
    setCurrentPage(i -1);
  } else
  if (i+1 < count())
  {
    setCurrentPage(i + 1);
  }
  setCurrentPage(i);
}

int ToolbarTabWidget::tabHeight() const
{
  int height = tabBar()->height();
  if (height < 2)
  {
      height = TQFontMetrics(KGlobalSettings::generalFont()).height() + 12;
  }
  return height;
}


QuantaToolBar::QuantaToolBar(TQWidget *parent, const char *name, bool honor_style, bool readConfig)
:KToolBar (parent, name=0, honor_style, readConfig)
{
  m_popupMenu = new KPopupMenu(this);
  m_toolbarTab = dynamic_cast<ToolbarTabWidget*>(parent->parentWidget());
  currentActionName = "";
  m_iconTextMenu = new KPopupMenu(this);
  m_iconTextMenu->setCheckable(true);
  m_iconTextMenu->insertItem(i18n("Icons Only"), 0);
  m_iconTextMenu->insertItem(i18n("Text Only"), 1);
  m_iconTextMenu->insertItem(i18n("Text Alongside Icons"), 2);
  m_iconTextMenu->insertItem(i18n("Text Under Icons"), 3);
  connect(m_iconTextMenu, TQT_SIGNAL(activated(int)), TQT_SLOT(slotIconTextChanged(int)));
  connect(m_iconTextMenu, TQT_SIGNAL(aboutToShow()), TQT_SLOT(slotIconTextMenuAboutToShow()));
  setIconText(ToolbarTabWidget::ref()->iconText(), false);
}

void QuantaToolBar::slotIconTextMenuAboutToShow()
{
  m_iconTextMenu->setItemChecked(0, false);
  m_iconTextMenu->setItemChecked(1, false);
  m_iconTextMenu->setItemChecked(2, false);
  m_iconTextMenu->setItemChecked(3, false);
  switch (ToolbarTabWidget::ref()->iconText()) 
  {
    case IconOnly: m_iconTextMenu->setItemChecked(0, true);
                   break;
    case TextOnly: m_iconTextMenu->setItemChecked(1, true);
                   break;
    case IconTextRight: m_iconTextMenu->setItemChecked(2, true);
                   break;
    case IconTextBottom: m_iconTextMenu->setItemChecked(3, true);
                   break;
  }  
}

void QuantaToolBar::slotIconTextChanged(int id)
{
  ToolbarTabWidget *toolbarTab = ToolbarTabWidget::ref();
  int width = toolbarTab->width();
  int bigHeight = iconSize() + TQFontMetrics(KGlobalSettings::toolBarFont()).height() + 10;
  int normalHeight = iconSize() + 10;
  for (int i = 0; i < toolbarTab->count(); i++)
  {
    QuantaToolBar *tb = static_cast<QuantaToolBar*>(toolbarTab->page(i));
    switch (id)
    {
      case 0: tb->setIconText(IconOnly); 
              tb->setGeometry(0,0, width, normalHeight);
              break;
      case 1: tb->setIconText(TextOnly); 
              tb->setGeometry(0,0, width, normalHeight);
              break;
      case 2: tb->setIconText(IconTextRight); 
              tb->setGeometry(0,0, width, normalHeight);
              break;
      case 3: tb->setIconText(IconTextBottom); 
              tb->setGeometry(0,0, width, bigHeight);
              break;
    }    
  }
  toolbarTab->setIconText(iconText()); 
  if (id == 3)
  {
    toolbarTab->setFixedHeight(toolbarTab->tabHeight() + height() + 3);
  } else
  {
    toolbarTab->setFixedHeight(toolbarTab->tabHeight() + height() + 3);
  }
}

void QuantaToolBar::mousePressEvent(TQMouseEvent *e)
{
  if (e->button() == Qt::RightButton)
  {
    m_popupMenu->clear();
    TQPoint p = e->globalPos();
    if (m_toolbarTab)
    {
      m_toolbarTab->tabUnderMouse = m_toolbarTab->id(m_toolbarTab->currentPageIndex());
      m_toolbarTab->tabUnderMouseLabel = m_toolbarTab->label(m_toolbarTab->currentPageIndex());
      m_popupMenu->insertTitle(i18n("Toolbar Menu") + " - "
                               + i18n(m_toolbarTab->tabUnderMouseLabel.utf8()));
      m_popupMenu->insertItem(i18n("New Action..."), m_toolbarTab, TQT_SIGNAL(newAction()));
      TQObjectList* childrenList = queryList("KToolBarButton");
      for (uint i = 0; i < childrenList->count(); i++)
      {
        KToolBarButton *w = static_cast<KToolBarButton*>(TQT_TQWIDGET(childrenList->at(i)));
        TQPoint p1 = w->parentWidget()->mapToGlobal(w->pos());
        TQPoint p2 = TQPoint(p1.x() + w->width(), p1.y()+w->height());
        if (TQRect(p1, p2).contains(p))
        {
          currentActionName = w->textLabel();
          TQString actionName = currentActionName;
          m_popupMenu->insertItem(i18n("Remove Action - %1").arg(actionName.replace('&',"&&")), this, TQT_SLOT(slotRemoveAction()));
          m_popupMenu->insertItem(i18n("Edit Action - %1").arg(actionName), this, TQT_SLOT(slotEditAction()));
          break;
        }
      }
      m_popupMenu->insertSeparator();
      m_popupMenu->insertItem(i18n("New Toolbar..."), m_toolbarTab, TQT_SIGNAL(addToolbar()));
      m_popupMenu->insertItem(i18n("Remove Toolbar"), m_toolbarTab, TQT_SLOT(slotRemoveToolbar()));
      m_popupMenu->insertItem(i18n("Rename Toolbar..."), m_toolbarTab, TQT_SLOT(slotRenameToolbar()));
      m_popupMenu->insertSeparator();
      m_popupMenu->insertItem( i18n("Text Position"), m_iconTextMenu);
      m_popupMenu->insertItem(SmallIconSet("configure_toolbars"), i18n("Configure Toolbars..."), m_toolbarTab, TQT_SLOT(slotEditToolbar()));
    }
    m_popupMenu->popup(p);
  }
}

void QuantaToolBar::slotEditAction()
{
  emit editAction(currentActionName);
}

void QuantaToolBar::slotRemoveAction()
{
    if ( KMessageBox::warningContinueCancel(this, i18n("<qt>Are you sure you want to remove the <b>%1</b> action?</qt>").arg(currentActionName),TQString(),KStdGuiItem::del()) == KMessageBox::Continue )
  {
    emit removeAction(m_toolbarTab->tabUnderMouse, currentActionName);
  }
}


#include "toolbartabwidget.moc"