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