20 #include "base/Debug.h" 30 m_lowerLimit(lowerLimit ? lowerLimit : 14),
31 m_upperLimit(upperLimit ? upperLimit : (m_lowerLimit * 5) / 2),
34 #ifdef DEBUG_SUBDIVIDING_MENU 35 cerr <<
"SubdividingMenu: constructed without title" << endl;
40 int upperLimit, QWidget *parent) :
46 #ifdef DEBUG_SUBDIVIDING_MENU 47 cerr <<
"SubdividingMenu: constructed with title \"" 48 << title <<
"\"" << endl;
65 #ifdef DEBUG_SUBDIVIDING_MENU 66 cerr <<
"SubdividingMenu::setEntries(" << title() <<
"): " 67 << entries.size() <<
" entries" << endl;
70 int total = int(entries.size());
75 QMenu *chunkMenu =
new QMenu();
76 chunkMenu->setTearOffEnabled(isTearOffEnabled());
78 QString firstNameInChunk;
79 QChar firstInitialInChunk;
80 bool discriminateStartInitial =
false;
84 auto comparator = [](QString s1, QString s2) ->
bool {
85 return QString::localeAwareCompare(s1, s2) < 0;
88 set<QString, decltype(comparator)> sortedEntries(comparator);
89 sortedEntries.insert(entries.begin(), entries.end());
91 for (
auto j = sortedEntries.begin(); j != sortedEntries.end(); ++j) {
93 #ifdef DEBUG_SUBDIVIDING_MENU 94 cerr <<
"SubdividingMenu::setEntries: entry is: " << j->toStdString() << endl;
102 QChar initial = (*j)[0].toUpper();
105 firstNameInChunk = *j;
106 firstInitialInChunk = initial;
107 #ifdef DEBUG_SUBDIVIDING_MENU 108 cerr <<
"starting new chunk at initial " << initial << endl;
112 #ifdef DEBUG_SUBDIVIDING_MENU 113 cerr <<
"count = "<< count <<
", upper limit = " <<
m_upperLimit << endl;
116 bool lastInChunk = (k == sortedEntries.end() ||
119 (*k)[0].toUpper() != initial)));
125 bool discriminateEndInitial = (k != sortedEntries.end() &&
126 (*k)[0].toUpper() == initial);
128 bool initialsEqual = (firstInitialInChunk == initial);
130 QString from = QString(
"%1").arg(firstInitialInChunk);
131 if (discriminateStartInitial ||
132 (discriminateEndInitial && initialsEqual)) {
133 from = firstNameInChunk.left(3);
136 QString to = QString(
"%1").arg(initial);
137 if (discriminateEndInitial ||
138 (discriminateStartInitial && initialsEqual)) {
144 if (from == to) menuText = from;
145 else menuText = tr(
"%1 - %2").arg(from).arg(to);
147 discriminateStartInitial = discriminateEndInitial;
149 chunkMenu->setTitle(menuText);
151 QMenu::addMenu(chunkMenu);
153 chunkMenu =
new QMenu();
154 chunkMenu->setTearOffEnabled(isTearOffEnabled());
160 if (count == 0)
delete chunkMenu;
167 SVCERR <<
"ERROR: SubdividingMenu::entriesAdded: setEntries was also called -- should use one mechanism or the other, but not both" << endl;
171 set<QString> entries;
173 entries.insert(i.first);
180 auto comparator = [](QString s1, QString s2) ->
bool {
181 return QString::localeAwareCompare(s1, s2) < 0;
183 set<QString, decltype(comparator)> sortedEntries(comparator);
184 for (
auto i: m_pendingEntries) {
185 sortedEntries.insert(i.first);
188 for (QString entry: sortedEntries) {
190 QObject *obj = m_pendingEntries[entry];
192 QMenu *menu =
dynamic_cast<QMenu *
>(obj);
198 QAction *action =
dynamic_cast<QAction *
>(obj);
205 m_pendingEntries.clear();
211 QString name = action->text();
219 #ifdef DEBUG_SUBDIVIDING_MENU 220 cerr <<
"SubdividingMenu::addAction(" << title() <<
" | " << name <<
"): not found in name-to-chunk map, adding to main menu" << endl;
222 QMenu::addAction(action);
226 #ifdef DEBUG_SUBDIVIDING_MENU 227 cerr <<
"SubdividingMenu::addAction(" << title() <<
" | " << name <<
"): found in name-to-chunk map for menu " <<
m_nameToChunkMenuMap[name]->title() << endl;
236 QAction *action =
new QAction(name,
this);
242 #ifdef DEBUG_SUBDIVIDING_MENU 243 cerr <<
"SubdividingMenu::addAction(" << title() <<
" | " << name <<
"): not found in name-to-chunk map, adding to main menu" << endl;
245 return QMenu::addAction(name);
248 #ifdef DEBUG_SUBDIVIDING_MENU 249 cerr <<
"SubdividingMenu::addAction(" << title() <<
" | " << name <<
"): found in name-to-chunk map for menu " <<
m_nameToChunkMenuMap[name]->title() << endl;
263 #ifdef DEBUG_SUBDIVIDING_MENU 264 cerr <<
"SubdividingMenu::addAction(" << title() <<
" | " << name <<
"): not found in name-to-chunk map, adding to main menu" << endl;
266 QMenu::addAction(action);
270 #ifdef DEBUG_SUBDIVIDING_MENU 271 cerr <<
"SubdividingMenu::addAction(" << title() <<
" | " << name <<
"): found in name-to-chunk map for menu " <<
m_nameToChunkMenuMap[name]->title() << endl;
279 QString name = menu->title();
287 #ifdef DEBUG_SUBDIVIDING_MENU 288 cerr <<
"SubdividingMenu::addMenu(" << title() <<
" | " << name <<
"): not found in name-to-chunk map, adding to main menu" << endl;
290 QMenu::addMenu(menu);
294 #ifdef DEBUG_SUBDIVIDING_MENU 295 cerr <<
"SubdividingMenu::addMenu(" << title() <<
" | " << name <<
"): found in name-to-chunk map for menu " <<
m_nameToChunkMenuMap[name]->title() << endl;
304 QMenu *menu =
new QMenu(name,
this);
305 menu->setTearOffEnabled(isTearOffEnabled());
311 #ifdef DEBUG_SUBDIVIDING_MENU 312 cerr <<
"SubdividingMenu::addMenu(" << title() <<
" | " << name <<
"): not found in name-to-chunk map, adding to main menu" << endl;
314 return QMenu::addMenu(name);
317 #ifdef DEBUG_SUBDIVIDING_MENU 318 cerr <<
"SubdividingMenu::addMenu(" << title() <<
" | " << name <<
"): found in name-to-chunk map for menu " <<
m_nameToChunkMenuMap[name]->title() << endl;
332 #ifdef DEBUG_SUBDIVIDING_MENU 333 cerr <<
"SubdividingMenu::addMenu(" << title() <<
" | " << name <<
"): not found in name-to-chunk map, adding to main menu" << endl;
335 QMenu::addMenu(menu);
339 #ifdef DEBUG_SUBDIVIDING_MENU 340 cerr <<
"SubdividingMenu::addMenu(" << title() <<
" | " << name <<
"): found in name-to-chunk map for menu " <<
m_nameToChunkMenuMap[name]->title() << endl;