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