1 /*
2 * Copyright 2009-2012, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7 #include "table/TableColumns.h"
8
9 #include <stdio.h>
10 #include <strings.h>
11
12 #include "util/TimeUtils.h"
13
14
15 // #pragma mark - DelegateBasedTableColumn
16
17
DelegateBasedTableColumn(BColumn * columnDelegate,int32 modelIndex,float width,float minWidth,float maxWidth,alignment align)18 DelegateBasedTableColumn::DelegateBasedTableColumn(BColumn* columnDelegate,
19 int32 modelIndex, float width, float minWidth, float maxWidth,
20 alignment align)
21 :
22 TableColumn(modelIndex, width, minWidth, maxWidth, align),
23 fColumnDelegate(columnDelegate)
24 {
25 }
26
27
~DelegateBasedTableColumn()28 DelegateBasedTableColumn::~DelegateBasedTableColumn()
29 {
30 }
31
32
33 void
DrawTitle(BRect rect,BView * targetView)34 DelegateBasedTableColumn::DrawTitle(BRect rect, BView* targetView)
35 {
36 fColumnDelegate->DrawTitle(rect, targetView);
37 }
38
39
40 void
GetColumnName(BString * into) const41 DelegateBasedTableColumn::GetColumnName(BString* into) const
42 {
43 fColumnDelegate->GetColumnName(into);
44 }
45
46
47 void
DrawValue(const BVariant & value,BRect rect,BView * targetView)48 DelegateBasedTableColumn::DrawValue(const BVariant& value, BRect rect,
49 BView* targetView)
50 {
51 fColumnDelegate->DrawField(PrepareField(value), rect, targetView);
52 }
53
54
55 float
GetPreferredWidth(const BVariant & value,BView * parent) const56 DelegateBasedTableColumn::GetPreferredWidth(const BVariant& value,
57 BView* parent) const
58 {
59 return fColumnDelegate->GetPreferredWidth(PrepareField(value), parent);
60 }
61
62
63 // #pragma mark - StringTableColumn
64
65
StringTableColumn(int32 modelIndex,const char * title,float width,float minWidth,float maxWidth,uint32 truncate,alignment align)66 StringTableColumn::StringTableColumn(int32 modelIndex, const char* title,
67 float width, float minWidth, float maxWidth, uint32 truncate,
68 alignment align)
69 :
70 DelegateBasedTableColumn(&fColumn, modelIndex, width, minWidth, maxWidth,
71 align),
72 fColumn(title, width, minWidth, maxWidth, truncate, align),
73 fField("")
74 {
75 }
76
77
~StringTableColumn()78 StringTableColumn::~StringTableColumn()
79 {
80 }
81
82
83 BField*
PrepareField(const BVariant & value) const84 StringTableColumn::PrepareField(const BVariant& value) const
85 {
86 fField.SetString(value.ToString());
87 fField.SetWidth(Width());
88 return &fField;
89 }
90
91
92 int
CompareValues(const BVariant & a,const BVariant & b)93 StringTableColumn::CompareValues(const BVariant& a, const BVariant& b)
94 {
95 return strcasecmp(a.ToString(), b.ToString());
96 }
97
98
99 // #pragma mark - BoolStringTableColumn
100
101
BoolStringTableColumn(int32 modelIndex,const char * title,float width,float minWidth,float maxWidth,const BString & trueString,const BString & falseString,uint32 truncate,alignment align)102 BoolStringTableColumn::BoolStringTableColumn(int32 modelIndex, const char* title,
103 float width, float minWidth, float maxWidth, const BString& trueString,
104 const BString& falseString, uint32 truncate, alignment align)
105 :
106 StringTableColumn(modelIndex, title, width, minWidth, maxWidth, truncate,
107 align),
108 fTrueString(trueString),
109 fFalseString(falseString)
110 {
111 }
112
113
114 BField*
PrepareField(const BVariant & value) const115 BoolStringTableColumn::PrepareField(const BVariant& value) const
116 {
117 return StringTableColumn::PrepareField(
118 BVariant(value.ToBool() ? fTrueString : fFalseString,
119 B_VARIANT_DONT_COPY_DATA));
120 }
121
122
123 int
CompareValues(const BVariant & a,const BVariant & b)124 BoolStringTableColumn::CompareValues(const BVariant& a, const BVariant& b)
125 {
126 bool aValue = a.ToBool();
127 return aValue == b.ToBool() ? 0 : (aValue ? 1 : -1);
128 }
129
130
131 // #pragma mark - Int32TableColumn
132
133
Int32TableColumn(int32 modelIndex,const char * title,float width,float minWidth,float maxWidth,uint32 truncate,alignment align)134 Int32TableColumn::Int32TableColumn(int32 modelIndex, const char* title,
135 float width, float minWidth, float maxWidth, uint32 truncate,
136 alignment align)
137 :
138 StringTableColumn(modelIndex, title, width, minWidth, maxWidth, truncate,
139 align)
140 {
141 }
142
143
144 BField*
PrepareField(const BVariant & value) const145 Int32TableColumn::PrepareField(const BVariant& value) const
146 {
147 char buffer[16];
148 snprintf(buffer, sizeof(buffer), "%" B_PRId32, value.ToInt32());
149 return StringTableColumn::PrepareField(
150 BVariant(buffer, B_VARIANT_DONT_COPY_DATA));
151 }
152
153
154 int
CompareValues(const BVariant & a,const BVariant & b)155 Int32TableColumn::CompareValues(const BVariant& a, const BVariant& b)
156 {
157 return a.ToInt32() - b.ToInt32();
158 }
159
160
161 // #pragma mark - Int64TableColumn
162
163
Int64TableColumn(int32 modelIndex,const char * title,float width,float minWidth,float maxWidth,uint32 truncate,alignment align)164 Int64TableColumn::Int64TableColumn(int32 modelIndex, const char* title,
165 float width, float minWidth, float maxWidth, uint32 truncate,
166 alignment align)
167 :
168 StringTableColumn(modelIndex, title, width, minWidth, maxWidth, truncate,
169 align)
170 {
171 }
172
173
174 BField*
PrepareField(const BVariant & value) const175 Int64TableColumn::PrepareField(const BVariant& value) const
176 {
177 char buffer[32];
178 snprintf(buffer, sizeof(buffer), "%" B_PRId64, value.ToInt64());
179 return StringTableColumn::PrepareField(
180 BVariant(buffer, B_VARIANT_DONT_COPY_DATA));
181 }
182
183
184 int
CompareValues(const BVariant & a,const BVariant & b)185 Int64TableColumn::CompareValues(const BVariant& a, const BVariant& b)
186 {
187 int64 diff = a.ToInt64() - b.ToInt64();
188 if (diff == 0)
189 return 0;
190 return diff < 0 ? -1 : 1;
191 }
192
193
194 // #pragma mark - BigtimeTableColumn
195
196
BigtimeTableColumn(int32 modelIndex,const char * title,float width,float minWidth,float maxWidth,bool invalidFirst,uint32 truncate,alignment align)197 BigtimeTableColumn::BigtimeTableColumn(int32 modelIndex, const char* title,
198 float width, float minWidth, float maxWidth, bool invalidFirst,
199 uint32 truncate, alignment align)
200 :
201 StringTableColumn(modelIndex, title, width, minWidth, maxWidth, truncate,
202 align),
203 fInvalidFirst(invalidFirst)
204 {
205 }
206
207
208 BField*
PrepareField(const BVariant & value) const209 BigtimeTableColumn::PrepareField(const BVariant& value) const
210 {
211 bigtime_t time = value.ToInt64();
212 if (time < 0) {
213 return StringTableColumn::PrepareField(
214 BVariant("-", B_VARIANT_DONT_COPY_DATA));
215 }
216
217 char buffer[64];
218 format_bigtime(time, buffer, sizeof(buffer));
219 return StringTableColumn::PrepareField(
220 BVariant(buffer, B_VARIANT_DONT_COPY_DATA));
221 }
222
223
224 int
CompareValues(const BVariant & _a,const BVariant & _b)225 BigtimeTableColumn::CompareValues(const BVariant& _a, const BVariant& _b)
226 {
227 bigtime_t a = _a.ToInt64();
228 bigtime_t b = _b.ToInt64();
229
230 if (a == b)
231 return 0;
232
233 if (a < 0)
234 return fInvalidFirst ? -1 : 1;
235 if (b < 0)
236 return fInvalidFirst ? 1 : -1;
237
238 return a - b < 0 ? -1 : 1;
239 }
240
241
242 // #pragma mark - NanotimeTableColumn
243
244
NanotimeTableColumn(int32 modelIndex,const char * title,float width,float minWidth,float maxWidth,bool invalidFirst,uint32 truncate,alignment align)245 NanotimeTableColumn::NanotimeTableColumn(int32 modelIndex, const char* title,
246 float width, float minWidth, float maxWidth, bool invalidFirst,
247 uint32 truncate, alignment align)
248 :
249 StringTableColumn(modelIndex, title, width, minWidth, maxWidth, truncate,
250 align),
251 fInvalidFirst(invalidFirst)
252 {
253 }
254
255
256 BField*
PrepareField(const BVariant & value) const257 NanotimeTableColumn::PrepareField(const BVariant& value) const
258 {
259 nanotime_t time = value.ToInt64();
260 if (time < 0) {
261 return StringTableColumn::PrepareField(
262 BVariant("-", B_VARIANT_DONT_COPY_DATA));
263 }
264
265 char buffer[64];
266 format_nanotime(time, buffer, sizeof(buffer));
267 return StringTableColumn::PrepareField(
268 BVariant(buffer, B_VARIANT_DONT_COPY_DATA));
269 }
270
271
272 int
CompareValues(const BVariant & _a,const BVariant & _b)273 NanotimeTableColumn::CompareValues(const BVariant& _a, const BVariant& _b)
274 {
275 nanotime_t a = _a.ToInt64();
276 nanotime_t b = _b.ToInt64();
277
278 if (a == b)
279 return 0;
280
281 if (a < 0)
282 return fInvalidFirst ? -1 : 1;
283 if (b < 0)
284 return fInvalidFirst ? 1 : -1;
285
286 return a - b < 0 ? -1 : 1;
287 }
288