|
| 1 | +//============================================================================= |
| 2 | +/// Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved. |
| 3 | +/// @author AMD Developer Tools Team |
| 4 | +/// @file |
| 5 | +/// @brief Implementation for our include directories view dialog |
| 6 | +//============================================================================= |
| 7 | + |
| 8 | +#include "include_directories_view.h" |
| 9 | + |
| 10 | +#include <algorithm> |
| 11 | +#include <cassert> |
| 12 | +#include <sstream> |
| 13 | + |
| 14 | +#include <QFileDialog> |
| 15 | +#include <QMessageBox> |
| 16 | +#include <QPushButton> |
| 17 | +#include <QVBoxLayout> |
| 18 | + |
| 19 | +#include "common_definitions.h" |
| 20 | + |
| 21 | +static const char* kStrIncludeDirDialogBrowseButton = "&Browse..."; ///< Select include directories browse button string. |
| 22 | +static const char* kStrIncludeDirDialogDirDoesNotExist = "This directory does not exist."; ///< Select include directory does not exist message string. |
| 23 | +static const char* kStrIncludeDirDialogDirAlreadySelected = "This directory is already selected."; ///< Select include directory already set message string. |
| 24 | +static const char* kStrIncludeDirDialogSelectDirTitle = "Select a directory"; ///< Select include directories file dialog title string. |
| 25 | + |
| 26 | +IncludeDirectoriesView::IncludeDirectoriesView(const QString& delimiter, const QString& window_title, const QIcon& window_icon, QWidget* parent) |
| 27 | + : OrderedListDialog(delimiter, window_icon, parent) |
| 28 | +{ |
| 29 | + // Initialize the browse button that gets inserted into the view. |
| 30 | + InitializeBrowseButton(); |
| 31 | + |
| 32 | + // Set the window title. |
| 33 | + setWindowTitle(window_title); |
| 34 | + |
| 35 | + // Connect the signals. |
| 36 | + ConnectSignals(); |
| 37 | + |
| 38 | + // Set the mouse cursor to the pointing hand cursor for various widgets. |
| 39 | + SetCursor(); |
| 40 | + |
| 41 | + // Set the button fonts. |
| 42 | + SetButtonFonts(); |
| 43 | + |
| 44 | + // Update various buttons. |
| 45 | + UpdateButtons(); |
| 46 | +} |
| 47 | + |
| 48 | +void IncludeDirectoriesView::ConnectSignals() |
| 49 | +{ |
| 50 | + assert(browse_push_button_ != nullptr); |
| 51 | + |
| 52 | + // Browse new include directory button. |
| 53 | + [[maybe_unused]] bool is_connected = |
| 54 | + connect(browse_push_button_, &QPushButton::clicked, this, &IncludeDirectoriesView::HandleIncludeFileLocationBrowseButtonClick); |
| 55 | + assert(is_connected); |
| 56 | +} |
| 57 | + |
| 58 | +void IncludeDirectoriesView::InitializeBrowseButton() |
| 59 | +{ |
| 60 | + // Create a new button to browse for new directories. |
| 61 | + browse_push_button_ = new QPushButton(this); |
| 62 | + browse_push_button_->setText(kStrIncludeDirDialogBrowseButton); |
| 63 | + |
| 64 | + assert(browse_push_button_ != nullptr); |
| 65 | + if (browse_push_button_ != nullptr) |
| 66 | + { |
| 67 | + // This index specifies where the browse button will get inserted in the vertical layout of buttons in the dialog. |
| 68 | + static const int kBrowseButtonInsertionIndex = 2; |
| 69 | + |
| 70 | + // Add the new browse button to the view. |
| 71 | + QVBoxLayout* vertical_buttons_layout = GetVerticalPushButtonsLayout(); |
| 72 | + assert(vertical_buttons_layout != nullptr); |
| 73 | + if (vertical_buttons_layout != nullptr) |
| 74 | + { |
| 75 | + vertical_buttons_layout->insertWidget(kBrowseButtonInsertionIndex, browse_push_button_); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void IncludeDirectoriesView::SetCursor() |
| 81 | +{ |
| 82 | + assert(browse_push_button_ != nullptr); |
| 83 | + if (browse_push_button_ != nullptr) |
| 84 | + { |
| 85 | + // Set the cursor to pointing hand cursor on the Browse button. |
| 86 | + browse_push_button_->setCursor(Qt::PointingHandCursor); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +void IncludeDirectoriesView::SetButtonFonts() |
| 91 | +{ |
| 92 | + // Create a font based on other QPushButtons in within the view. |
| 93 | + QFont font = GetDeletePushButton()->font(); |
| 94 | + font.setPointSize(kButtonFontPointSize); |
| 95 | + |
| 96 | + assert(browse_push_button_ != nullptr); |
| 97 | + if (browse_push_button_ != nullptr) |
| 98 | + { |
| 99 | + // Update font size for all the buttons. |
| 100 | + browse_push_button_->setFont(font); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +void IncludeDirectoriesView::SetDefaultFolderPath(std::string default_folder_path) |
| 105 | +{ |
| 106 | + default_folder_path_ = default_folder_path; |
| 107 | +} |
| 108 | + |
| 109 | +void IncludeDirectoriesView::HandleIncludeFileLocationBrowseButtonClick(bool /* checked */) |
| 110 | +{ |
| 111 | + // Create a file chooser dialog. |
| 112 | + QFileDialog file_dialog; |
| 113 | + |
| 114 | + // Get the last entry from the directories list and open the dialog there. |
| 115 | + std::string latest_path = default_folder_path_; |
| 116 | + // If the user has an item selected, use that as starting path. |
| 117 | + // Otherwise use the last item in the list since it is probably most recent. |
| 118 | + QListWidgetItem* item = GetItemsListWidget()->currentItem(); |
| 119 | + if (item != nullptr && !item->text().isEmpty() && QDir(item->text()).exists()) |
| 120 | + { |
| 121 | + latest_path = item->text().toStdString(); |
| 122 | + } |
| 123 | + else if (items_list_.size() > 0) |
| 124 | + { |
| 125 | + latest_path = items_list_.at(items_list_.size() - 1).toStdString(); |
| 126 | + } |
| 127 | + |
| 128 | + bool should_show_find_directory_dialog = true; |
| 129 | + |
| 130 | + while (should_show_find_directory_dialog) |
| 131 | + { |
| 132 | + QString selected_directory = |
| 133 | + QFileDialog::getExistingDirectory(this, tr(kStrIncludeDirDialogSelectDirTitle), latest_path.c_str(), QFileDialog::ShowDirsOnly); |
| 134 | + |
| 135 | + // If the user did not select an entry, don't update anything, just exit the loop. |
| 136 | + if (selected_directory.isEmpty()) |
| 137 | + { |
| 138 | + should_show_find_directory_dialog = false; |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + // If not a duplicate selection, update the list widget. |
| 143 | + if (!items_list_.contains(selected_directory)) |
| 144 | + { |
| 145 | + item = GetItemsListWidget()->currentItem(); |
| 146 | + if (item == nullptr) |
| 147 | + { |
| 148 | + // There was no selected item, |
| 149 | + // so make sure there is a final entry that is empty, and edit that. |
| 150 | + if (GetItemsListWidget()->count() > 0) |
| 151 | + { |
| 152 | + item = GetItemsListWidget()->item(GetItemsListWidget()->count() - 1); |
| 153 | + } |
| 154 | + |
| 155 | + if (item == nullptr || !item->text().isEmpty()) |
| 156 | + { |
| 157 | + // Add an empty row to the dialog |
| 158 | + InsertBlankItem(); |
| 159 | + assert(GetItemsListWidget()->count() > 0); |
| 160 | + |
| 161 | + // Use the empty row for the new item |
| 162 | + item = GetItemsListWidget()->item(GetItemsListWidget()->count() - 1); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + assert(item != nullptr); |
| 167 | + item->setText(selected_directory); |
| 168 | + |
| 169 | + // If the last item in the UI is no longer empty, add another item. |
| 170 | + item = GetItemsListWidget()->item(GetItemsListWidget()->count() - 1); |
| 171 | + if (item == nullptr || !item->text().isEmpty()) |
| 172 | + { |
| 173 | + // Add an empty row to the dialog |
| 174 | + InsertBlankItem(); |
| 175 | + } |
| 176 | + |
| 177 | + // Don't show the find directory dialog again. |
| 178 | + should_show_find_directory_dialog = false; |
| 179 | + } |
| 180 | + else |
| 181 | + { |
| 182 | + // Display an error message when a duplicate directory is selected. |
| 183 | + QMessageBox::critical(this, "Error", kStrIncludeDirDialogDirAlreadySelected); |
| 184 | + |
| 185 | + // Make the user try again. |
| 186 | + should_show_find_directory_dialog = true; |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + // Update move buttons. |
| 192 | + UpdateButtons(); |
| 193 | +} |
| 194 | + |
| 195 | +void IncludeDirectoriesView::OnListItemChanged(QListWidgetItem* item) |
| 196 | +{ |
| 197 | + // Block signals from the list widget. |
| 198 | + GetItemsListWidget()->blockSignals(true); |
| 199 | + |
| 200 | + editing_invalid_entry_ = false; |
| 201 | + |
| 202 | + // Process the newly-entered data. |
| 203 | + if (item != nullptr) |
| 204 | + { |
| 205 | + QString new_directory = item->text(); |
| 206 | + |
| 207 | + bool directory_exists = QDir(new_directory).exists(); |
| 208 | + bool directory_duplicate = items_list_.contains(new_directory); |
| 209 | + bool directory_value_empty = new_directory.isEmpty(); |
| 210 | + |
| 211 | + int item_row = GetItemsListWidget()->row(item); |
| 212 | + |
| 213 | + if (directory_value_empty && item_row != GetItemsListWidget()->count() - 1) |
| 214 | + { |
| 215 | + // The user has emptied out the entry, so delete it. |
| 216 | + // Simulate a click on the delete button to remove the entry from the UI. |
| 217 | + GetDeletePushButton()->click(); |
| 218 | + } |
| 219 | + else |
| 220 | + { |
| 221 | + // If the new directory exists, and it is not a duplicate entry, update local data. |
| 222 | + if (!directory_exists || directory_duplicate) |
| 223 | + { |
| 224 | + // Display an error message box. |
| 225 | + if (!directory_exists) |
| 226 | + { |
| 227 | + QMessageBox::critical(this, "Error", kStrIncludeDirDialogDirDoesNotExist); |
| 228 | + } |
| 229 | + else if (directory_duplicate) |
| 230 | + { |
| 231 | + QMessageBox::critical(this, "Error", kStrIncludeDirDialogDirAlreadySelected); |
| 232 | + } |
| 233 | + |
| 234 | + editing_invalid_entry_ = true; |
| 235 | + } |
| 236 | + |
| 237 | + // Update local data. |
| 238 | + if (item_row < items_list_.count()) |
| 239 | + { |
| 240 | + items_list_[item_row] = new_directory; |
| 241 | + } |
| 242 | + else |
| 243 | + { |
| 244 | + items_list_.append(new_directory); |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + // Update tool tips. |
| 249 | + UpdateToolTips(); |
| 250 | + |
| 251 | + // Unblock signals from the list widget. |
| 252 | + GetItemsListWidget()->blockSignals(false); |
| 253 | + |
| 254 | + // Update buttons. |
| 255 | + UpdateButtons(); |
| 256 | + } |
| 257 | +} |
0 commit comments