DirectoryList.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
  3. *
  4. * This file is part of the QCAD project.
  5. *
  6. * QCAD is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * QCAD is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with QCAD.
  18. */
  19. /**
  20. * Fills the list widget "listWidgetName", a child of pageWidget with
  21. * directories (from where?)
  22. */
  23. function DirectoryList(pageWidget, listWidgetName, fileDlgTitle) {
  24. this.doInit(pageWidget, listWidgetName, fileDlgTitle);
  25. }
  26. DirectoryList.prototype.doInit = function(pageWidget, listWidgetName, fileDlgTitle) {
  27. var widgets = getWidgets(pageWidget);
  28. var listWidget = widgets[listWidgetName];
  29. listWidget.dragDropMode = QAbstractItemView.InternalMove;
  30. listWidget.itemClicked.connect(this, function(item) {
  31. var btRemove = widgets["btRemove"];
  32. btRemove.enabled = true;
  33. this.checkButtonStatus(listWidget, widgets);
  34. });
  35. var btAdd = widgets["btAdd"];
  36. btAdd.clicked.connect(this, function() {
  37. // getExistingDirectory is broken in KDE when it uses the native dialog.
  38. /*
  39. var dir = QFileDialog.getExistingDirectory(this, fileDlgTitle);
  40. if (dir.length===0) {
  41. return;
  42. }
  43. */
  44. var fileDialog = new QFileDialog(pageWidget, fileDlgTitle, "", "");
  45. fileDialog.setOption(QFileDialog.DontUseNativeDialog, getDontUseNativeDialog());
  46. fileDialog.fileMode = QFileDialog.Directory;
  47. fileDialog.setOption(QFileDialog.ShowDirsOnly, true);
  48. fileDialog.setOption(QFileDialog.DontResolveSymlinks, true);
  49. if (!isNull(QFileDialog.DontUseCustomDirectoryIcons)) {
  50. fileDialog.setOption(QFileDialog.DontUseCustomDirectoryIcons, true);
  51. }
  52. if (!fileDialog.exec()) {
  53. destr(fileDialog);
  54. EAction.activateMainWindow();
  55. return;
  56. }
  57. var files = fileDialog.selectedFiles();
  58. if (files.length===0) {
  59. destr(fileDialog);
  60. EAction.activateMainWindow();
  61. return;
  62. }
  63. var dir = files[0];
  64. var found = false;
  65. for ( var i = 0; i < listWidget.count; ++i) {
  66. var item = listWidget.item(i);
  67. if (item.text() == dir) {
  68. found = true;
  69. break;
  70. }
  71. }
  72. if (!found) {
  73. listWidget.addItem(dir);
  74. } else {
  75. var text = qsTr("The folder\n\"%1\"\nis already in the list.").arg(dir);
  76. var appWin = EAction.getMainWindow();
  77. QMessageBox.information(appWin, qsTr("Folder not added"), text);
  78. }
  79. this.checkButtonStatus(listWidget, widgets);
  80. });
  81. var btRemove = widgets["btRemove"];
  82. btRemove.enabled = false;
  83. btRemove.clicked.connect(this, function() {
  84. var currentRow = listWidget.currentRow;
  85. if (currentRow == -1) {
  86. this.checkButtonStatus(listWidget, widgets);
  87. return;
  88. }
  89. listWidget.model().removeRow(currentRow);
  90. if (listWidget.count == 0) {
  91. btRemove.enabled = false;
  92. }
  93. this.checkButtonStatus(listWidget, widgets);
  94. });
  95. var btUp = widgets["btUp"];
  96. btUp.clicked.connect(this, function() {
  97. var currentRow = listWidget.currentRow;
  98. if (currentRow == -1) {
  99. return;
  100. }
  101. var item = listWidget.takeItem(currentRow);
  102. if (isNull(item)) {
  103. return;
  104. }
  105. listWidget.insertItem(currentRow - 1, item);
  106. listWidget.setCurrentItem(item);
  107. this.checkButtonStatus(listWidget, widgets);
  108. });
  109. var btDown = widgets["btDown"];
  110. btDown.clicked.connect(this, function() {
  111. var currentRow = listWidget.currentRow;
  112. if (currentRow == -1) {
  113. return;
  114. }
  115. var item = listWidget.takeItem(currentRow);
  116. if (isNull(item)) {
  117. return;
  118. }
  119. listWidget.insertItem(currentRow + 1, item);
  120. listWidget.setCurrentItem(item);
  121. this.checkButtonStatus(listWidget, widgets);
  122. });
  123. };
  124. DirectoryList.prototype.checkButtonStatus = function(listWidget, widgets) {
  125. var currentRow = listWidget.currentRow;
  126. var btUp = widgets["btUp"];
  127. if (currentRow < 1) {
  128. btUp.enabled = false;
  129. } else {
  130. btUp.enabled = true;
  131. }
  132. var btDown = widgets["btDown"];
  133. if (currentRow == listWidget.count - 1) {
  134. btDown.enabled = false;
  135. } else {
  136. btDown.enabled = true;
  137. }
  138. };