| 272 | } |
| 273 | |
| 274 | QString QG_CommandWidget::getRootCommand( const QStringList & cmdList, const QString & typed ) { |
| 275 | //do we have to check for empty cmdList? |
| 276 | if(cmdList.empty()) return QString(); |
| 277 | |
| 278 | //find the shortest string in cmdList |
| 279 | auto const& shortestString = * std::min_element(cmdList.begin(), cmdList.end(), |
| 280 | [](QString const& a, QString const& b) -> bool |
| 281 | { |
| 282 | return a.size() < b.size(); |
| 283 | } |
| 284 | ); |
| 285 | int const lengthShortestString = shortestString.size(); |
| 286 | |
| 287 | // Now we parse the cmdList list, character of each item by character. |
| 288 | int low = typed.length(); |
| 289 | int high = lengthShortestString + 1; |
| 290 | |
| 291 | while(high > low + 1) { |
| 292 | int mid = (high + low)/2; |
| 293 | bool common = true; |
| 294 | |
| 295 | QString const& proposal = shortestString.left(mid); |
| 296 | for(auto const& substring: cmdList) { |
| 297 | if(!substring.startsWith(proposal)) { |
| 298 | common = false; |
| 299 | break; |
| 300 | } |
| 301 | } |
| 302 | if(common) { |
| 303 | low = mid; |
| 304 | } |
| 305 | else { |
| 306 | high = mid; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // As we assign just before mid value to low (if strings are common), we can use it as parameter for left. |
| 311 | // If not common -> low value does not changes, even if escaping from the while. This avoids weird behaviors like continuing completion when pressing tab. |
| 312 | return shortestString.left(low); |
| 313 | |
| 314 | } |
| 315 | |
| 316 | void QG_CommandWidget::chooseCommandFile() |
| 317 | { |