File: xPshBtnTable.cpp

package info (click to toggle)
xirc 2.0-3
  • links: PTS
  • area: contrib
  • in suites: hamm, slink
  • size: 2,624 kB
  • ctags: 2,093
  • sloc: cpp: 21,094; makefile: 1,033
file content (226 lines) | stat: -rw-r--r-- 7,769 bytes parent folder | download
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
/***************************************************************************
**    xPshBtnTable.cpp  $Revision: 1.5 $ - $Name: V2-18 $ 
**    xFrame for handling 2 Dimensional Tables of buttons
**
**    Copyright (C) 1996 Joseph Croft <jcroft@unicomp.net>
**
**    This library is free software; you can redistribute it and/or
**    modify it under the terms of the GNU Library General Public
**    License as published by the Free Software Foundation; either
**    version 2 of the License, or (at your option) any later version.
**
**    This library is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**    Library General Public License for more details.
**
**    You should have received a copy of the GNU Library General Public
**    License along with this library; if not, write to the Free
**    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
 ***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <qfont.h>
#include "xPshBtnTable.h"

static int dbg = 0;

xPshBtnTable::xPshBtnTable(xWidgetResInfo *pPRes, QWidget *pParent,
                           const char *pName) :
              xFrame((wdtRes = new xWidgetResInfo(pPRes, QString(""),
                                                  QString(""))),
                     pParent, pName)
{
   wdtResPrv = new xWidgetResInfo(pPRes);
   setDefPallet(this, wdtRes);
   setDefFont(this, wdtRes);

//   setFont(QFont("helvetica", 12, QFont::Bold));
   setAlignment(xALIGN_Horz);
   setMargins(3, 3);
   setResizeMode(xSPACE_Resize);
   btnList.setAutoDelete(TRUE);
}

xPshBtnTable::~xPshBtnTable()
{
   xPshBtnFrame *pBtnFrame;

   for (xPshBtn *pBtn = btnList.first(); pBtn; pBtn = btnList.first())
      btnList.remove(pBtn);

   for (pBtnFrame = btnFrameList.first(); pBtnFrame; pBtnFrame = btnFrameList.first())
   {
      btnFrameList.remove(pBtnFrame);
      delete pBtnFrame;
   }
}

void xPshBtnTable::arrangeButtons(int cols)
{
   xPshBtn *pBtn, *pBtnNew;
   xPshBtnFrame *pBtnFrame;
   int btnCnt = 0, maxHeight = 0, maxWidth = 0;
   int diffColRow = 0, extraBtns = 0, bestFit, x, y, xm, ym;

   //
   // Start by getting a button count and the width
   // of the widest button
   //
   if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():Counting Buttons\n");
   for (pBtn = btnList.first(); pBtn; pBtn = btnList.next())
   {
      btnCnt++;
      maxWidth = (pBtn->width() > maxWidth) ? pBtn->width() : maxWidth;
      maxHeight = (pBtn->height() > maxHeight) ? pBtn->height() : maxHeight;
   }
   if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():have %d Buttons\n", btnCnt);

   //
   // Set all buttons to the width of the widest button
   //
   for (pBtn = btnList.first(); pBtn; pBtn = btnList.next())
      pBtn->resize(maxWidth, maxHeight);

   //
   // If they didn't specify the number of columns, do your
   // best at figuring out how many
   //
   if (cols == 0)
   {
      diffColRow = extraBtns = 65536;
      bestFit = 1;
      for (x = 2; x < btnCnt; x++)
      {
         if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():Trying %d Rows\n",
                                  x);
         cols = btnCnt / x;
         y = btnCnt % x; 
         if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():Makes %d Cols w/ %d left over\n",
                                   cols, y);
         if (y <= extraBtns)
         {
            if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():abs(cols - x): %d < diffColRow: %d\n",
                                      abs(cols - x), diffColRow);
            if (abs(cols - x) <= diffColRow)
            {
               diffColRow = abs(cols - x);
               bestFit = cols;
               if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():diffColRow = %d\n",
                                         diffColRow);
               if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():Best fit yet!\n");
            }
            extraBtns = y;
         }
      }
      cols = bestFit;
      if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():Settled on %d Cols\n",
                                cols);
   }

   //
   // Now delete any existing Button frames by first unlinking its
   // buttons. Then create as many Button frames as columns and
   // Place the buttons into them
   //
   if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():Deleting existing frames\n");
   for (pBtnFrame = btnFrameList.last(); pBtnFrame;)
   {
      btnFrameList.remove(pBtnFrame);
      delete pBtnFrame;
   }

   //
   // Now create all new frames to hold the buttons we have
   //
   if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():Adding Buttons\n");
   if (dbg) fflush(stdout);
   for (x = 0, pBtn = btnList.first(); x < cols && pBtn != 0; x++)
   {
      if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():New button Frame %d\n", x);
      if (dbg) fflush(stdout);
      pBtnFrame = new xPshBtnFrame(wdtResPrv, this);
      pBtnFrame->setAlignment(xALIGN_Vert);
      margins(ym, xm);
      pBtnFrame->setMargins(0, xm);
      btnFrameList.append(pBtnFrame);
      if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():Adding Buttons to frame\n");
         if (dbg) fflush(stdout);
      for (y = 0; y < btnCnt / cols && pBtn; y++, pBtn = btnList.next())
      {
         if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():Adding Button %d to frame\n", y);
         if (dbg) fflush(stdout);
         pBtnNew = pBtnFrame->addButton(pBtn->text(), pBtn->id());
         pBtnNew->resize(maxWidth, pBtn->height());
         if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():Button Added!\n");
         if (dbg) fflush(stdout);
      }
      if (extraBtns > 0 && pBtn != NULL)
      {
         if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():Adding Extra Button %d to frame\n",
                                  extraBtns);
         if (dbg) fflush(stdout);
         pBtnNew = pBtnFrame->addButton(pBtn->text(), pBtn->id());
         pBtnNew->resize(maxWidth, pBtn->height());
         if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():Extra Button Added!\n");
         if (dbg) fflush(stdout);
         extraBtns--;
         pBtn = btnList.next();
      }
      if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():Connecting Button Frame signals\n");
      if (dbg) fflush(stdout);
      connect(pBtnFrame, SIGNAL(clicked(int)), this, SLOT(buttonClicked(int)));
      if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():Adding Button Frame to this\n");
      if (dbg) fflush(stdout);
      pBtnFrame->fitFrame();
      addWidget(pBtnFrame);
   }
   if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():Fitting this frame!\n");
   if (dbg) fflush(stdout);
   fitFrame();
   if (dbg) fprintf(stdout, "xPshBtnTable::arrangeButtons():Exit\n");
   if (dbg) fflush(stdout);
}

void xPshBtnTable::addButton(xPshBtn *pBtn, int id)
{
   pBtn->setId(id);
   addButton(pBtn);
}

void xPshBtnTable::addButton(xPshBtn *pButton)
{
   btnList.append(pButton);
//   pButton->setFont(font());
//   addWidget(pButton);
}

xPshBtn *xPshBtnTable::addButton(const char *pBtnName, int id)
{
   xPshBtn *pBtn;
   
   if ((pBtn = new xPshBtn(wdtRes, NULL, pBtnName, pBtnName, id)) != NULL)
   {
      addButton(pBtn);
   }
   return(pBtn);
}

void xPshBtnTable::rmvButton(int id)
{
   xPshBtn *pBtn;

   for (pBtn = btnList.first(); pBtn; pBtn = btnList.next())
   {
      if (id == pBtn->id())
         btnList.removeRef(pBtn);
   }
}

void xPshBtnTable::buttonClicked(int id)
{
   emit clicked(id);
}

#include "xPshBtnTable.moc"