xref: /haiku/src/apps/debuganalyzer/gui/table/TableColumns.cpp (revision 23f179da55b1bd1ba84fbf3d3c56947e2c8d0aca)
1 /*
2  * Copyright 2009, 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 
11 
12 // #pragma mark - DelegateBasedTableColumn
13 
14 
15 DelegateBasedTableColumn::DelegateBasedTableColumn(BColumn* columnDelegate,
16 	int32 modelIndex, float width, float minWidth, float maxWidth,
17 	alignment align)
18 	:
19 	TableColumn(modelIndex, width, minWidth, maxWidth, align),
20 	fColumnDelegate(columnDelegate)
21 {
22 }
23 
24 
25 DelegateBasedTableColumn::~DelegateBasedTableColumn()
26 {
27 }
28 
29 
30 void
31 DelegateBasedTableColumn::DrawTitle(BRect rect, BView* targetView)
32 {
33 	fColumnDelegate->DrawTitle(rect, targetView);
34 }
35 
36 
37 void
38 DelegateBasedTableColumn::GetColumnName(BString* into) const
39 {
40 	fColumnDelegate->GetColumnName(into);
41 }
42 
43 
44 void
45 DelegateBasedTableColumn::DrawValue(const BVariant& value, BRect rect,
46 	BView* targetView)
47 {
48 	fColumnDelegate->DrawField(PrepareField(value), rect, targetView);
49 }
50 
51 
52 float
53 DelegateBasedTableColumn::GetPreferredWidth(const BVariant& value,
54 	BView* parent) const
55 {
56 	return fColumnDelegate->GetPreferredWidth(PrepareField(value), parent);
57 }
58 
59 
60 // #pragma mark - StringTableColumn
61 
62 
63 StringTableColumn::StringTableColumn(int32 modelIndex, const char* title,
64 	float width, float minWidth, float maxWidth, uint32 truncate,
65 	alignment align)
66 	:
67 	DelegateBasedTableColumn(&fColumn, modelIndex, width, minWidth, maxWidth,
68 		align),
69 	fColumn(title, width, minWidth, maxWidth, truncate, align),
70 	fField("")
71 {
72 }
73 
74 
75 StringTableColumn::~StringTableColumn()
76 {
77 }
78 
79 
80 BField*
81 StringTableColumn::PrepareField(const BVariant& value) const
82 {
83 	fField.SetString(value.ToString());
84 	fField.SetWidth(Width());
85 	return &fField;
86 }
87 
88 
89 int
90 StringTableColumn::CompareValues(const BVariant& a, const BVariant& b)
91 {
92 	return strcasecmp(a.ToString(), b.ToString());
93 }
94 
95 
96 // #pragma mark - BoolStringTableColumn
97 
98 
99 BoolStringTableColumn::BoolStringTableColumn(int32 modelIndex, const char* title,
100 	float width, float minWidth, float maxWidth, const BString& trueString,
101 	const BString& falseString, uint32 truncate, alignment align)
102 	:
103 	StringTableColumn(modelIndex, title, width, minWidth, maxWidth, truncate,
104 		align),
105 	fTrueString(trueString),
106 	fFalseString(falseString)
107 {
108 }
109 
110 
111 BField*
112 BoolStringTableColumn::PrepareField(const BVariant& value) const
113 {
114 	return StringTableColumn::PrepareField(
115 		BVariant(value.ToBool() ? fTrueString : fFalseString,
116 			B_VARIANT_DONT_COPY_DATA));
117 }
118 
119 
120 int
121 BoolStringTableColumn::CompareValues(const BVariant& a, const BVariant& b)
122 {
123 	bool aValue = a.ToBool();
124 	return aValue == b.ToBool() ? 0 : (aValue ? 1 : -1);
125 }
126 
127 
128 // #pragma mark - Int32TableColumn
129 
130 
131 Int32TableColumn::Int32TableColumn(int32 modelIndex, const char* title,
132 	float width, float minWidth, float maxWidth, uint32 truncate,
133 	alignment align)
134 	:
135 	StringTableColumn(modelIndex, title, width, minWidth, maxWidth, truncate,
136 		align)
137 {
138 }
139 
140 
141 BField*
142 Int32TableColumn::PrepareField(const BVariant& value) const
143 {
144 	char buffer[16];
145 	snprintf(buffer, sizeof(buffer), "%ld", value.ToInt32());
146 	return StringTableColumn::PrepareField(
147 		BVariant(buffer, B_VARIANT_DONT_COPY_DATA));
148 }
149 
150 
151 int
152 Int32TableColumn::CompareValues(const BVariant& a, const BVariant& b)
153 {
154 	return a.ToInt32() - b.ToInt32();
155 }
156 
157 
158 // #pragma mark - Int64TableColumn
159 
160 
161 Int64TableColumn::Int64TableColumn(int32 modelIndex, const char* title,
162 	float width, float minWidth, float maxWidth, uint32 truncate,
163 	alignment align)
164 	:
165 	StringTableColumn(modelIndex, title, width, minWidth, maxWidth, truncate,
166 		align)
167 {
168 }
169 
170 
171 BField*
172 Int64TableColumn::PrepareField(const BVariant& value) const
173 {
174 	char buffer[32];
175 	snprintf(buffer, sizeof(buffer), "%lld", value.ToInt64());
176 	return StringTableColumn::PrepareField(
177 		BVariant(buffer, B_VARIANT_DONT_COPY_DATA));
178 }
179 
180 
181 int
182 Int64TableColumn::CompareValues(const BVariant& a, const BVariant& b)
183 {
184 	int64 diff = a.ToInt64() - b.ToInt64();
185 	if (diff == 0)
186 		return 0;
187 	return diff < 0 ? -1 : 1;
188 }
189 
190 
191 // #pragma mark - BigtimeTableColumn
192 
193 
194 BigtimeTableColumn::BigtimeTableColumn(int32 modelIndex, const char* title,
195 	float width, float minWidth, float maxWidth, bool invalidFirst,
196 	uint32 truncate, alignment align)
197 	:
198 	StringTableColumn(modelIndex, title, width, minWidth, maxWidth, truncate,
199 		align),
200 	fInvalidFirst(invalidFirst)
201 {
202 }
203 
204 
205 BField*
206 BigtimeTableColumn::PrepareField(const BVariant& value) const
207 {
208 	bigtime_t time = value.ToInt64();
209 	if (time < 0) {
210 		return StringTableColumn::PrepareField(
211 			BVariant("-", B_VARIANT_DONT_COPY_DATA));
212 	}
213 
214 	int micros = int(time % 1000000);
215 	time /= 1000000;
216 	int seconds = int(time % 60);
217 	time /= 60;
218 	int minutes = int(time % 60);
219 	time /= 60;
220 
221 	char buffer[64];
222 	snprintf(buffer, sizeof(buffer), "%02lld:%02d:%02d:%06d", time, minutes,
223 		seconds, micros);
224 	return StringTableColumn::PrepareField(
225 		BVariant(buffer, B_VARIANT_DONT_COPY_DATA));
226 }
227 
228 
229 int
230 BigtimeTableColumn::CompareValues(const BVariant& _a, const BVariant& _b)
231 {
232 	bigtime_t a = _a.ToInt64();
233 	bigtime_t b = _b.ToInt64();
234 
235 	if (a == b)
236 		return 0;
237 
238 	if (a < 0)
239 		return fInvalidFirst ? -1 : 1;
240 	if (b < 0)
241 		return fInvalidFirst ? 1 : -1;
242 
243 	return a - b < 0 ? -1 : 1;
244 }
245