Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: chrome/browser/custom_home_pages_table_model.cc

Issue 7044136: Support drag&drop for startup pages (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/browser/custom_home_pages_table_model.h" 5 #include "chrome/browser/custom_home_pages_table_model.h"
6 6
7 #include "base/i18n/rtl.h" 7 #include "base/i18n/rtl.h"
8 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/prefs/pref_service.h" 9 #include "chrome/browser/prefs/pref_service.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 entries_[i].url = urls[i]; 59 entries_[i].url = urls[i];
60 entries_[i].title.erase(); 60 entries_[i].title.erase();
61 entries_[i].icon.reset(); 61 entries_[i].icon.reset();
62 LoadTitleAndFavicon(&(entries_[i])); 62 LoadTitleAndFavicon(&(entries_[i]));
63 } 63 }
64 // Complete change, so tell the view to just rebuild itself. 64 // Complete change, so tell the view to just rebuild itself.
65 if (observer_) 65 if (observer_)
66 observer_->OnModelChanged(); 66 observer_->OnModelChanged();
67 } 67 }
68 68
69 /**
70 * Move a number of existing entries to a new position, reordering the table.
71 *
72 * We determine the range of elements affected by the move, save the moved
73 * elements, compact the remaining ones, and re-insert moved elements.
74 */
75 void CustomHomePagesTableModel::MoveURLs(int insert_before,
76 const std::vector<int>& index_list)
csilv 2011/06/13 22:57:02 Should we be concerned about this not working prop
groby-ooo-7-16 2011/06/13 23:45:27 Since the underlying SelectionModel uses Object.ke
csilv 2011/06/14 00:06:12 A comment is prefect, thanks. On 2011/06/13 23:45
77 {
78 DCHECK(insert_before >= 0 && insert_before <= RowCount());
79
80 // The range of elements that needs to be reshuffled is [ |first|, |last| ).
csilv 2011/06/13 22:57:02 nit picky: the end of the comment opens with a bra
groby-ooo-7-16 2011/06/13 23:45:27 Actually intentional - that's the standard range n
csilv 2011/06/14 00:06:12 Ah, that thought crossed my mind. (I'm not famili
81 int first = std::min(insert_before,index_list.front());
csilv 2011/06/13 22:57:02 style nit: space after comma
groby-ooo-7-16 2011/06/13 23:45:27 Done.
82 int last = std::max(insert_before, index_list.back() + 1);
83
84 // Save the dragged elements. Also, adjust insertion point if it is before a
85 // dragged element.
86 std::vector<Entry> moved_entries;
87 for (size_t i = 0; i < index_list.size(); ++i) {
88 moved_entries.push_back(entries_[index_list[i]]);
89 if (index_list[i] == insert_before)
90 insert_before++;
91 }
92
93 // Compact the range between beginning and insertion point, moving downwards.
94 size_t skip_count = 0;
95 for (int i = first; i < insert_before; ++i) {
96 if (skip_count < index_list.size() && index_list[skip_count] == i)
97 skip_count++;
98 else
99 entries_[i - skip_count]=entries_[i];
100 }
101
102 // Moving items down created a gap. We start compacting up after it.
103 first = insert_before;
104 insert_before -= skip_count;
105
106 // Now compact up for elements after the insertion point.
107 skip_count = 0;
108 for (int i = last - 1; i >= first; --i) {
109 if (skip_count < index_list.size() &&
110 index_list[index_list.size() - skip_count - 1] == i) {
111 skip_count++;
112 } else {
113 entries_[i + skip_count] = entries_[i];
114 }
115 }
116
117 // Insert moved elements.
118 std::copy(moved_entries.begin(), moved_entries.end(),
119 entries_.begin() + insert_before);
120
121 // Possibly large change, so tell the view to just rebuild itself.
122 if (observer_)
123 observer_->OnModelChanged();
124 }
125
69 void CustomHomePagesTableModel::Add(int index, const GURL& url) { 126 void CustomHomePagesTableModel::Add(int index, const GURL& url) {
70 DCHECK(index >= 0 && index <= RowCount()); 127 DCHECK(index >= 0 && index <= RowCount());
71 entries_.insert(entries_.begin() + static_cast<size_t>(index), Entry()); 128 entries_.insert(entries_.begin() + static_cast<size_t>(index), Entry());
72 entries_[index].url = url; 129 entries_[index].url = url;
73 LoadTitleAndFavicon(&(entries_[index])); 130 LoadTitleAndFavicon(&(entries_[index]));
74 if (observer_) 131 if (observer_)
75 observer_->OnItemsAdded(index, 1); 132 observer_->OnItemsAdded(index, 1);
76 } 133 }
77 134
78 void CustomHomePagesTableModel::Remove(int index) { 135 void CustomHomePagesTableModel::Remove(int index) {
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 return NULL; 287 return NULL;
231 } 288 }
232 289
233 string16 CustomHomePagesTableModel::FormattedURL(int row) const { 290 string16 CustomHomePagesTableModel::FormattedURL(int row) const {
234 std::string languages = 291 std::string languages =
235 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); 292 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages);
236 string16 url = net::FormatUrl(entries_[row].url, languages); 293 string16 url = net::FormatUrl(entries_[row].url, languages);
237 url = base::i18n::GetDisplayStringInLTRDirectionality(url); 294 url = base::i18n::GetDisplayStringInLTRDirectionality(url);
238 return url; 295 return url;
239 } 296 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698