forked from ozcanyarimdunya/python_mini_projeler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7beca1a
commit b382cf0
Showing
6 changed files
with
459 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Searchable ListWidget | ||
|
||
A list widget with searching/filtering implemented | ||
|
||
|Initial|Searched| | ||
|-------|--------| | ||
|![initial](./assets/1.png)|![initial](./assets/2.png)| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import json | ||
import os | ||
import sys | ||
|
||
from PyQt5 import uic | ||
from PyQt5.QtCore import Qt | ||
from PyQt5.QtWidgets import ( | ||
QMainWindow, | ||
QApplication, | ||
QLineEdit, | ||
QListWidget, | ||
QListWidgetItem | ||
) | ||
|
||
|
||
class AppMainWindow(QMainWindow): | ||
txt_search: QLineEdit | ||
lw_list: QListWidget | ||
list_data = list() | ||
|
||
_path_form = 'ui/form.ui' | ||
_path_all = 'data/all.json' | ||
_path_checked = 'data/checked.json' | ||
|
||
def __init__(self, flags=None, *args, **kwargs): | ||
super().__init__(flags, *args, **kwargs) | ||
self.initialise() | ||
|
||
def initialise(self): | ||
self.initialise_ui() | ||
self.initialise_data() | ||
self.initialise_event() | ||
self.initialise_widgets() | ||
|
||
def initialise_ui(self): | ||
with open(self.path_form) as fp: | ||
uic.loadUi(self.path_form, self) | ||
|
||
def initialise_data(self): | ||
with open(self.path_all, 'r') as fp_all, open(self.path_checked) as fp_checked: | ||
_all = json.load(fp_all) | ||
_checked = json.load(fp_checked) | ||
self.list_data = list( | ||
map(lambda item: | ||
dict(check_state=Qt.Checked, text=item) if item in _checked | ||
else dict(check_state=Qt.Unchecked, text=item), | ||
_all) | ||
) | ||
|
||
def initialise_event(self): | ||
self.txt_search.textChanged.connect(lambda q: self.on_search(q)) | ||
self.lw_list.itemChanged.connect(lambda item: self.on_lw_changed(item)) | ||
|
||
def initialise_widgets(self): | ||
self.update_lw(data=self.list_data) | ||
|
||
def update_lw(self, data): | ||
self.lw_list.clear() | ||
for each in data: | ||
item = QListWidgetItem() | ||
item.setText(each['text']) | ||
item.setFlags(item.flags() | Qt.ItemIsUserCheckable) | ||
item.setCheckState(each['check_state']) | ||
self.lw_list.addItem(item) | ||
|
||
def on_lw_changed(self, item: QListWidgetItem): | ||
self.list_data = list( | ||
map(lambda each: | ||
dict(check_state=item.checkState(), text=item.text()) if each['text'] == item.text() else each, | ||
self.list_data) | ||
) | ||
|
||
def on_search(self, query): | ||
data = list( | ||
filter(lambda q: | ||
q['text'].lower().__contains__(query.lower()), | ||
self.list_data) | ||
) | ||
self.update_lw(data) | ||
|
||
def closeEvent(self, event): | ||
mapped = list( | ||
map(lambda item: | ||
item['text'], | ||
filter(lambda item: item['check_state'] == Qt.Checked, self.list_data)) | ||
) | ||
with open(self._path_checked, 'w') as fp: | ||
json.dump(mapped, fp) | ||
|
||
@property | ||
def path_form(self): | ||
if not os.path.isfile(self._path_all): | ||
raise Exception('Cannot start without a ui file') | ||
return self._path_form | ||
|
||
@property | ||
def path_all(self): | ||
if not os.path.isfile(self._path_all): | ||
with open(self._path_all, 'w') as fp: | ||
json.dump([], fp) | ||
return self._path_all | ||
|
||
@property | ||
def path_checked(self): | ||
if not os.path.isfile(self._path_checked): | ||
with open(self._path_checked, 'w') as fp: | ||
json.dump([], fp) | ||
return self._path_checked | ||
|
||
|
||
if __name__ == "__main__": | ||
app = QApplication(sys.argv) | ||
form = AppMainWindow() | ||
form.show() | ||
sys.exit(app.exec_()) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
[ | ||
"Afghanistan", | ||
"Albania", | ||
"Algeria", | ||
"American Samoa", | ||
"Andorra", | ||
"Angola", | ||
"Anguilla", | ||
"Antarctica", | ||
"Antigua and Barbuda", | ||
"Argentina", | ||
"Armenia", | ||
"Aruba", | ||
"Australia", | ||
"Austria", | ||
"Azerbaijan", | ||
"Bahamas", | ||
"Bahrain", | ||
"Bangladesh", | ||
"Barbados", | ||
"Belarus", | ||
"Belgium", | ||
"Belize", | ||
"Benin", | ||
"Bermuda", | ||
"Bhutan", | ||
"Bolivia", | ||
"Bosnia and Herzegovina", | ||
"Botswana", | ||
"Bouvet Island", | ||
"Brazil", | ||
"British Indian Ocean Territory", | ||
"Brunei", | ||
"Bulgaria", | ||
"Burkina Faso", | ||
"Burundi", | ||
"Cambodia", | ||
"Cameroon", | ||
"Canada", | ||
"Cape Verde", | ||
"Cayman Islands", | ||
"Central African Republic", | ||
"Chad", | ||
"Chile", | ||
"China", | ||
"Christmas Island", | ||
"Cocos (Keeling) Islands", | ||
"Colombia", | ||
"Comoros", | ||
"Congo", | ||
"The Democratic Republic of Congo", | ||
"Cook Islands", | ||
"Costa Rica", | ||
"Ivory Coast", | ||
"Croatia", | ||
"Cuba", | ||
"Cyprus", | ||
"Czech Republic", | ||
"Denmark", | ||
"Djibouti", | ||
"Dominica", | ||
"Dominican Republic", | ||
"East Timor", | ||
"Ecuador", | ||
"Egypt", | ||
"England", | ||
"El Salvador", | ||
"Equatorial Guinea", | ||
"Eritrea", | ||
"Estonia", | ||
"Ethiopia", | ||
"Falkland Islands", | ||
"Faroe Islands", | ||
"Fiji Islands", | ||
"Finland", | ||
"France", | ||
"French Guiana", | ||
"French Polynesia", | ||
"French Southern territories", | ||
"Gabon", | ||
"Gambia", | ||
"Georgia", | ||
"Germany", | ||
"Ghana", | ||
"Gibraltar", | ||
"Greece", | ||
"Greenland", | ||
"Grenada", | ||
"Guadeloupe", | ||
"Guam", | ||
"Guatemala", | ||
"Guernsey", | ||
"Guinea", | ||
"Guinea-Bissau", | ||
"Guyana", | ||
"Haiti", | ||
"Heard Island and McDonald Islands", | ||
"Holy See (Vatican City State)", | ||
"Honduras", | ||
"Hong Kong", | ||
"Hungary", | ||
"Iceland", | ||
"India", | ||
"Indonesia", | ||
"Iran", | ||
"Iraq", | ||
"Ireland", | ||
"Israel", | ||
"Isle of Man", | ||
"Italy", | ||
"Jamaica", | ||
"Japan", | ||
"Jersey", | ||
"Jordan", | ||
"Kazakhstan", | ||
"Kenya", | ||
"Kiribati", | ||
"Kuwait", | ||
"Kyrgyzstan", | ||
"Laos", | ||
"Latvia", | ||
"Lebanon", | ||
"Lesotho", | ||
"Liberia", | ||
"Libyan Arab Jamahiriya", | ||
"Liechtenstein", | ||
"Lithuania", | ||
"Luxembourg", | ||
"Macao", | ||
"North Macedonia", | ||
"Madagascar", | ||
"Malawi", | ||
"Malaysia", | ||
"Maldives", | ||
"Mali", | ||
"Malta", | ||
"Marshall Islands", | ||
"Martinique", | ||
"Mauritania", | ||
"Mauritius", | ||
"Mayotte", | ||
"Mexico", | ||
"Micronesia, Federated States of", | ||
"Moldova", | ||
"Monaco", | ||
"Mongolia", | ||
"Montserrat", | ||
"Montenegro", | ||
"Morocco", | ||
"Mozambique", | ||
"Myanmar", | ||
"Namibia", | ||
"Nauru", | ||
"Nepal", | ||
"Netherlands", | ||
"Netherlands Antilles", | ||
"New Caledonia", | ||
"New Zealand", | ||
"Nicaragua", | ||
"Niger", | ||
"Nigeria", | ||
"Niue", | ||
"Norfolk Island", | ||
"North Korea", | ||
"Northern Ireland", | ||
"Northern Mariana Islands", | ||
"Norway", | ||
"Oman", | ||
"Pakistan", | ||
"Palau", | ||
"Palestine", | ||
"Panama", | ||
"Papua New Guinea", | ||
"Paraguay", | ||
"Peru", | ||
"Philippines", | ||
"Pitcairn", | ||
"Poland", | ||
"Portugal", | ||
"Puerto Rico", | ||
"Qatar", | ||
"Reunion", | ||
"Romania", | ||
"Russian Federation", | ||
"Rwanda", | ||
"Saint Helena", | ||
"Saint Kitts and Nevis", | ||
"Saint Lucia", | ||
"Saint Pierre and Miquelon", | ||
"Saint Vincent and the Grenadines", | ||
"Samoa", | ||
"San Marino", | ||
"Sao Tome and Principe", | ||
"Saudi Arabia", | ||
"Scotland", | ||
"Senegal", | ||
"Serbia", | ||
"Seychelles", | ||
"Sierra Leone", | ||
"Singapore", | ||
"Slovakia", | ||
"Slovenia", | ||
"Solomon Islands", | ||
"Somalia", | ||
"South Africa", | ||
"South Georgia and the South Sandwich Islands", | ||
"South Korea", | ||
"South Sudan", | ||
"Spain", | ||
"SriLanka", | ||
"Sudan", | ||
"Suriname", | ||
"Svalbard and Jan Mayen", | ||
"Swaziland", | ||
"Sweden", | ||
"Switzerland", | ||
"Syria", | ||
"Tajikistan", | ||
"Tanzania", | ||
"Thailand", | ||
"Timor-Leste", | ||
"Togo", | ||
"Tokelau", | ||
"Tonga", | ||
"Trinidad and Tobago", | ||
"Tunisia", | ||
"Turkey", | ||
"Turkmenistan", | ||
"Turks and Caicos Islands", | ||
"Tuvalu", | ||
"Uganda", | ||
"Ukraine", | ||
"United Arab Emirates", | ||
"United Kingdom", | ||
"United States", | ||
"United States Minor Outlying Islands", | ||
"Uruguay", | ||
"Uzbekistan", | ||
"Vanuatu", | ||
"Venezuela", | ||
"Vietnam", | ||
"Virgin Islands, British", | ||
"Virgin Islands, U.S.", | ||
"Wales", | ||
"Wallis and Futuna", | ||
"Western Sahara", | ||
"Yemen", | ||
"Yugoslavia", | ||
"Zambia", | ||
"Zimbabwe" | ||
] |
Oops, something went wrong.