| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/controls/table/table_view.h" | 5 #include "ui/views/controls/table/table_view.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 UpdateVisibleColumnSizes(); | 227 UpdateVisibleColumnSizes(); |
| 228 PreferredSizeChanged(); | 228 PreferredSizeChanged(); |
| 229 SchedulePaint(); | 229 SchedulePaint(); |
| 230 if (header_) | 230 if (header_) |
| 231 header_->SchedulePaint(); | 231 header_->SchedulePaint(); |
| 232 } | 232 } |
| 233 | 233 |
| 234 void TableView::ToggleSortOrder(int visible_column_index) { | 234 void TableView::ToggleSortOrder(int visible_column_index) { |
| 235 DCHECK(visible_column_index >= 0 && | 235 DCHECK(visible_column_index >= 0 && |
| 236 visible_column_index < static_cast<int>(visible_columns_.size())); | 236 visible_column_index < static_cast<int>(visible_columns_.size())); |
| 237 if (!visible_columns_[visible_column_index].column.sortable) | 237 const ui::TableColumn& column = visible_columns_[visible_column_index].column; |
| 238 if (!column.sortable) |
| 238 return; | 239 return; |
| 239 const int column_id = visible_columns_[visible_column_index].column.id; | |
| 240 SortDescriptors sort(sort_descriptors_); | 240 SortDescriptors sort(sort_descriptors_); |
| 241 if (!sort.empty() && sort[0].column_id == column_id) { | 241 if (!sort.empty() && sort[0].column_id == column.id) { |
| 242 sort[0].ascending = !sort[0].ascending; | 242 if (sort[0].ascending == column.initial_sort_is_ascending) { |
| 243 // First toggle inverts the order. |
| 244 sort[0].ascending = !sort[0].ascending; |
| 245 } else { |
| 246 // Second toggle clears the sort. |
| 247 sort.clear(); |
| 248 } |
| 243 } else { | 249 } else { |
| 244 SortDescriptor descriptor(column_id, visible_columns_[ | 250 SortDescriptor descriptor(column.id, column.initial_sort_is_ascending); |
| 245 visible_column_index].column.initial_sort_is_ascending); | |
| 246 sort.insert(sort.begin(), descriptor); | 251 sort.insert(sort.begin(), descriptor); |
| 247 // Only persist two sort descriptors. | 252 // Only persist two sort descriptors. |
| 248 if (sort.size() > 2) | 253 if (sort.size() > 2) |
| 249 sort.resize(2); | 254 sort.resize(2); |
| 250 } | 255 } |
| 251 SetSortDescriptors(sort); | 256 SetSortDescriptors(sort); |
| 252 } | 257 } |
| 253 | 258 |
| 259 void TableView::SetSortDescriptors(const SortDescriptors& sort_descriptors) { |
| 260 sort_descriptors_ = sort_descriptors; |
| 261 SortItemsAndUpdateMapping(); |
| 262 if (header_) |
| 263 header_->SchedulePaint(); |
| 264 } |
| 265 |
| 254 bool TableView::IsColumnVisible(int id) const { | 266 bool TableView::IsColumnVisible(int id) const { |
| 255 for (size_t i = 0; i < visible_columns_.size(); ++i) { | 267 for (size_t i = 0; i < visible_columns_.size(); ++i) { |
| 256 if (visible_columns_[i].column.id == id) | 268 if (visible_columns_[i].column.id == id) |
| 257 return true; | 269 return true; |
| 258 } | 270 } |
| 259 return false; | 271 return false; |
| 260 } | 272 } |
| 261 | 273 |
| 262 void TableView::AddColumn(const ui::TableColumn& col) { | 274 void TableView::AddColumn(const ui::TableColumn& col) { |
| 263 DCHECK(!HasColumn(col.id)); | 275 DCHECK(!HasColumn(col.id)); |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 633 void TableView::OnBlur() { | 645 void TableView::OnBlur() { |
| 634 SchedulePaintForSelection(); | 646 SchedulePaintForSelection(); |
| 635 } | 647 } |
| 636 | 648 |
| 637 void TableView::NumRowsChanged() { | 649 void TableView::NumRowsChanged() { |
| 638 SortItemsAndUpdateMapping(); | 650 SortItemsAndUpdateMapping(); |
| 639 PreferredSizeChanged(); | 651 PreferredSizeChanged(); |
| 640 SchedulePaint(); | 652 SchedulePaint(); |
| 641 } | 653 } |
| 642 | 654 |
| 643 void TableView::SetSortDescriptors(const SortDescriptors& sort_descriptors) { | |
| 644 sort_descriptors_ = sort_descriptors; | |
| 645 SortItemsAndUpdateMapping(); | |
| 646 if (header_) | |
| 647 header_->SchedulePaint(); | |
| 648 } | |
| 649 | |
| 650 void TableView::SortItemsAndUpdateMapping() { | 655 void TableView::SortItemsAndUpdateMapping() { |
| 651 if (!is_sorted()) { | 656 if (!is_sorted()) { |
| 652 view_to_model_.clear(); | 657 view_to_model_.clear(); |
| 653 model_to_view_.clear(); | 658 model_to_view_.clear(); |
| 654 } else { | 659 } else { |
| 655 const int row_count = RowCount(); | 660 const int row_count = RowCount(); |
| 656 view_to_model_.resize(row_count); | 661 view_to_model_.resize(row_count); |
| 657 model_to_view_.resize(row_count); | 662 model_to_view_.resize(row_count); |
| 658 for (int i = 0; i < row_count; ++i) | 663 for (int i = 0; i < row_count; ++i) |
| 659 view_to_model_[i] = i; | 664 view_to_model_[i] = i; |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 952 if (tooltip) | 957 if (tooltip) |
| 953 *tooltip = text; | 958 *tooltip = text; |
| 954 if (tooltip_origin) { | 959 if (tooltip_origin) { |
| 955 tooltip_origin->SetPoint(cell_bounds.x(), | 960 tooltip_origin->SetPoint(cell_bounds.x(), |
| 956 cell_bounds.y() + kTextVerticalPadding); | 961 cell_bounds.y() + kTextVerticalPadding); |
| 957 } | 962 } |
| 958 return true; | 963 return true; |
| 959 } | 964 } |
| 960 | 965 |
| 961 } // namespace views | 966 } // namespace views |
| OLD | NEW |