📄 searchpage.cpp
字号:
if(Node->CheckState == csCheckedNormal)
Networks += ((TNetworkNodeData*)NetworkTree->GetNodeData(Node))->Network->GetName() + ",";
}
Config->SetValue("search/networks",Networks);
}
NetworkTree->EndUpdate();
// reenable search button
StartSearchBtn->Enabled = true;
}
//---------------------------------------------------------------------------
void TSearchForm::RunSearch(AnsiString Query, TSearchRealm Realm, AnsiString Network)
{
bool FilterWords = Config->GetValueInt("search/filter_banned_words");
bool FilterExtensions = Config->GetValueInt("search/filter_banned_extensions");
// create new search
TSearch* Search = Engine->NewSearch(Query.c_str(), Realm, Network.c_str(), TMetaData(), FilterWords, FilterExtensions);
// calculate caption title here because it requries access to page control
int MaxCaptionWidth = 120;
AnsiString TabCaption = Query;
int Width = ResultTabCtrl->Canvas->TextWidth(TabCaption);
for(int Len = Query.Length(); Len > 0 && Width > MaxCaptionWidth; Len--) {
TabCaption = Query.SubString(0,Len) + "...";
Width = ResultTabCtrl->Canvas->TextWidth(TabCaption);
}
// create result form
TSearchResultForm* ResultForm = new TSearchResultForm(ResultPanel,Search,TabCaption);
ResultForm->Parent = ResultPanel;
ResultForm->Align = alClient;
// set XP Theme support for new form
if(MainForm->ThemeManager)
MainForm->ThemeManager->CollectForms(ResultForm);
// add to tab control
int i = ResultTabCtrl->Tabs->AddObject("",(TObject*)ResultForm);
// add realm icon image list
if(Config->GetValueInt("search/realm_icons_on_tabs"))
ResultTabCtrl->Images = MainForm->FileImageList;
else
ResultTabCtrl->Images = NULL;
ResultTabCtrl->Tabs->Strings[i] = TabCaption;
ResultTabCtrl->TabIndex = i;
ResultTabCtrlChange(NULL);
// make sure tabs and result form are shown
ResultTabPanel->Visible = true;
ResultForm->Show();
// start search
Search->Start();
}
void TSearchForm::CloseSearch(TSearchResultForm* ResultForm)
{
int SelectedTab = ResultTabCtrl->TabIndex;
int Index = -1;
for(int i=0; i < ResultTabCtrl->Tabs->Count; i++) {
if ((TSearchResultForm*)ResultTabCtrl->Tabs->Objects[i] == ResultForm) {
Index = i;
break;
}
}
if(Index == -1)
return;
ResultForm->Release();
if(SelectedTab >= Index)
SelectedTab = SelectedTab > 0 ? SelectedTab - 1 : 0;
ResultTabCtrl->Tabs->Delete(Index);
ResultTabCtrl->TabIndex = SelectedTab;
ResultTabCtrlChange(NULL);
if(ResultTabCtrl->Tabs->Count == 0)
ResultTabPanel->Visible = false;
}
void TSearchForm::SetTabCaption(TSearchResultForm* ResultForm, AnsiString& Title)
{
for(int i=0; i < ResultTabCtrl->Tabs->Count; i++) {
if ((TSearchResultForm*)ResultTabCtrl->Tabs->Objects[i] == ResultForm) {
ResultTabCtrl->Tabs->Strings[i] = Title;
break;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TSearchForm::StartSearchBtnClick(TObject *Sender)
{
TSearchRealm Realm = (TSearchRealm)RealmRadioGroup->Items->Objects[RealmRadioGroup->ItemIndex];
// save realm
Config->SetValue("search/realm", TSearch::RealmToString(Realm));
// check if user entered a magnet
string Query = QueryCombo->Text.c_str();
Query = string_trim(Query);
if(TMagnet::IsMagnetURI(Query)) {
QueryCombo->Text = "";
TMagnetForm* MagnetDlg = new TMagnetForm(this,Query);
MagnetDlg->ShowModal();
delete MagnetDlg;
return;
}
// replace useless tokens in query with space
if(Realm != SRSource && Realm != SRUser) {
string Query = QueryCombo->Text.c_str();
string CleanQuery;
int p1, p2 = 0;
for(;;) {
if((p1 = Query.find_first_not_of(BAD_QUERY_TOKENS,p2)) == -1)
break;
if((p2 = Query.find_first_of(BAD_QUERY_TOKENS,p1)) == -1) {
CleanQuery += Query.substr(p1) + " ";
break;
}
CleanQuery += Query.substr(p1,p2-p1) + " ";
}
// remove trailing space
if(CleanQuery.length() > 0)
CleanQuery.erase(CleanQuery.length()-1);
// show the user what we did and proceed
QueryCombo->Text = CleanQuery.c_str();
}
if(QueryCombo->Text.Length() < 2) {
// TRANSLATOR: Message box when trying to search for less than 2 characters.
MessageDlg(_("Please use a search term that is at least 2 characters long"),
mtError,TMsgDlgButtons() << mbOK,0);
return;
}
if(QueryCombo->Items->IndexOf(QueryCombo->Text) == -1) {
// keep history within limits
QueryCombo->Items->BeginUpdate();
while(QueryCombo->Items->Count >= MAX_QUERY_HISTORY_SIZE)
QueryCombo->Items->Delete(QueryCombo->Items->Count - 1);
QueryCombo->Items->EndUpdate();
// add query to top of history
QueryCombo->Items->Insert(0,QueryCombo->Text);
}
// if no networks were selected, select all
bool NoNetworks = true;
TVirtualNode* Node = NetworkTree->GetFirst();
for(; Node; Node=NetworkTree->GetNextSibling(Node)) {
if(Node->CheckState == csCheckedNormal) {
NoNetworks = false;
break;
}
}
if(NoNetworks)
NetworkTree->CheckState[NetworkTree->GetFirst()] = csCheckedNormal;
// get networks to search on
AnsiString Networks;
if(NetworkTree->GetFirst()->CheckState != csCheckedNormal) {
Node = NetworkTree->GetFirst();
for(Node=NetworkTree->GetNextSibling(Node); Node; Node=NetworkTree->GetNextSibling(Node)) {
if(Node->CheckState == csCheckedNormal)
Networks += AnsiString(((TNetworkNodeData*)NetworkTree->GetNodeData(Node))->Network->GetName().c_str()) + " ";
}
}
// override network to openft only for torrents
if(Realm == SRTorrent)
Networks = "OpenFT";
RunSearch(QueryCombo->Text, Realm, Networks);
// disable search button until user changed a search parameter
StartSearchBtn->Enabled = false;
}
void __fastcall TSearchForm::QueryComboKeyPress(TObject *Sender, char &Key)
{
if(Key == 13) {
Key = 0; // don't add enter to edit
QueryCombo->SelStart = QueryCombo->Text.Length(); // don't delete selected text
if(StartSearchBtn->Enabled)
StartSearchBtn->Click();
}
}
//---------------------------------------------------------------------------
void __fastcall TSearchForm::QueryComboClick(TObject *Sender)
{
// TRANSLATOR: Search history list entry which erases search history when selected.
if(QueryCombo->ItemIndex != 0 || QueryCombo->Items->Strings[QueryCombo->ItemIndex] != _("=> Purge Searches"))
return;
QueryCombo->Items->Delete(0);
QueryCombo->ClearSelection();
// TRANSLATOR: Message box confirming erasing of search history.
if(MessageDlg(_("Are you sure you want to purge searches?"),
mtConfirmation,TMsgDlgButtons() <<mbYes << mbNo,0) != mrYes) {
return;
}
QueryCombo->Clear();
}
void __fastcall TSearchForm::QueryComboDropDown(TObject *Sender)
{
// TRANSLATOR: Search history list entry which erases search history when selected.
QueryCombo->Items->Insert(0,_("=> Purge Searches"));
}
void __fastcall TSearchForm::QueryComboCloseUp(TObject *Sender)
{
if(QueryCombo->ItemIndex != 0)
QueryCombo->Items->Delete(0);
}
//---------------------------------------------------------------------------
void __fastcall TSearchForm::RealmRadioGroupClick(TObject *Sender)
{
StartSearchBtn->Enabled = true;
}
void __fastcall TSearchForm::QueryComboChange(TObject *Sender)
{
StartSearchBtn->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TSearchForm::HideQueryBtnClick(TObject *Sender)
{
if(LeftPanel->Visible) {
LeftPanel->Visible = false;
HideQueryBtn->Glyph->LoadFromResourceName((unsigned int)HInstance,"SLIDE_RIGHT_BMP");
} else {
LeftPanel->Visible = true;
HideQueryBtn->Glyph->LoadFromResourceName((unsigned int)HInstance,"SLIDE_LEFT_BMP");
QueryCombo->SetFocus();
}
}
//---------------------------------------------------------------------------
void __fastcall TSearchForm::ResultTabCtrlChange(TObject *Sender)
{
// move result form associated with tab to foreground
if(ResultTabCtrl->TabIndex == -1)
return;
TSearchResultForm* ResultForm = (TSearchResultForm*)ResultTabCtrl->Tabs->Objects[ResultTabCtrl->TabIndex];
ResultForm->Visible = true;
ResultForm->BringToFront();
}
void __fastcall TSearchForm::ResultTabCtrlGetImageIndex(TObject *Sender,
int TabIndex, int &ImageIndex)
{
if(ResultTabCtrl->Tabs->Count <= TabIndex)
return;
TSearchResultForm* ResultForm = (TSearchResultForm*)ResultTabCtrl->Tabs->Objects[TabIndex];
ImageIndex = MainForm->IconManager->GetSearchRealmIconIndex(ResultForm->GetSearch()->GetRealm());
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -