📄 searchdialog.cs
字号:
//
// cbDesc
//
this.cbDesc.Checked = true;
this.cbDesc.CheckState = System.Windows.Forms.CheckState.Checked;
this.cbDesc.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.cbDesc.Location = new System.Drawing.Point(6, 42);
this.cbDesc.Name = "cbDesc";
this.cbDesc.Size = new System.Drawing.Size(176, 18);
this.cbDesc.TabIndex = 1;
this.cbDesc.Text = "Photo descriptions";
//
// cbTitle
//
this.cbTitle.Checked = true;
this.cbTitle.CheckState = System.Windows.Forms.CheckState.Checked;
this.cbTitle.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.cbTitle.Location = new System.Drawing.Point(6, 19);
this.cbTitle.Name = "cbTitle";
this.cbTitle.Size = new System.Drawing.Size(176, 18);
this.cbTitle.TabIndex = 0;
this.cbTitle.Text = "Photo titles";
//
// lvResults
//
this.lvResults.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.colTitle,
this.colAlbum,
this.colTime});
this.lvResults.FullRowSelect = true;
this.lvResults.HideSelection = false;
this.lvResults.Location = new System.Drawing.Point(310, 84);
this.lvResults.MultiSelect = false;
this.lvResults.Name = "lvResults";
this.lvResults.Size = new System.Drawing.Size(242, 303);
this.lvResults.TabIndex = 12;
this.lvResults.View = System.Windows.Forms.View.Details;
this.lvResults.SelectedIndexChanged += new System.EventHandler(this.lvResults_SelectedIndexChanged);
this.lvResults.DoubleClick += new System.EventHandler(this.lvResults_DoubleClick);
this.lvResults.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lvResults_MouseUp);
this.lvResults.MouseMove += new System.Windows.Forms.MouseEventHandler(this.lvResults_MouseMove);
this.lvResults.KeyDown += new System.Windows.Forms.KeyEventHandler(this.lvResults_KeyDown);
//
// colTitle
//
this.colTitle.Text = "Title";
this.colTitle.Width = 82;
//
// colAlbum
//
this.colAlbum.Text = "Album";
this.colAlbum.Width = 78;
//
// colTime
//
this.colTime.Text = "Time taken";
this.colTime.Width = 77;
//
// tvAlbums
//
this.tvAlbums.CheckBoxes = true;
this.tvAlbums.Location = new System.Drawing.Point(12, 84);
this.tvAlbums.Name = "tvAlbums";
this.tvAlbums.Size = new System.Drawing.Size(188, 302);
this.tvAlbums.TabIndex = 13;
this.tvAlbums.MouseUp += new System.Windows.Forms.MouseEventHandler(this.tvAlbums_MouseUp);
//
// contMenu
//
this.contMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.miOpenPhoto,
this.miOpenAlbum,
this.miPrint,
this.menuItem3,
this.miPInfo});
this.contMenu.Popup += new System.EventHandler(this.contMenu_Popup);
//
// miOpenPhoto
//
this.miOpenPhoto.Index = 0;
this.miOpenPhoto.Text = "Open photo...";
this.miOpenPhoto.Click += new System.EventHandler(this.miOpenPhoto_Click);
//
// miOpenAlbum
//
this.miOpenAlbum.Index = 1;
this.miOpenAlbum.Text = "Open album";
this.miOpenAlbum.Click += new System.EventHandler(this.miOpenAlbum_Click);
//
// miPrint
//
this.miPrint.Index = 2;
this.miPrint.Text = "Print...";
this.miPrint.Click += new System.EventHandler(this.miPrint_Click);
//
// menuItem3
//
this.menuItem3.Index = 3;
this.menuItem3.Text = "-";
//
// miPInfo
//
this.miPInfo.Index = 4;
this.miPInfo.Text = "Photo-Info";
this.miPInfo.Click += new System.EventHandler(this.miPInfo_Click);
//
// ttPhotoInfo
//
this.ttPhotoInfo.AutoPopDelay = 5000;
this.ttPhotoInfo.InitialDelay = 500;
this.ttPhotoInfo.ReshowDelay = 100;
//
// tToolTip
//
this.tToolTip.Enabled = true;
this.tToolTip.Tick += new System.EventHandler(this.tToolTip_Tick);
//
// contMenuTv
//
this.contMenuTv.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.miCheckAll,
this.miUncheckAll});
this.contMenuTv.Popup += new System.EventHandler(this.contMenuTv_Popup);
//
// miCheckAll
//
this.miCheckAll.Index = 0;
this.miCheckAll.Text = "Check all";
this.miCheckAll.Click += new System.EventHandler(this.miCheckAll_Click);
//
// miUncheckAll
//
this.miUncheckAll.Index = 1;
this.miUncheckAll.Text = "Uncheck all";
this.miUncheckAll.Click += new System.EventHandler(this.miUncheckAll_Click);
//
// SearchDialog
//
this.ClientSize = new System.Drawing.Size(564, 592);
this.Controls.Add(this.tvAlbums);
this.Controls.Add(this.lvResults);
this.Controls.Add(this.gbWhat);
this.Controls.Add(this.pbPreview);
this.Controls.Add(this.tbSearchText);
this.Controls.Add(this.lbEnterText);
this.Controls.Add(this.pbAnim);
this.Controls.Add(this.cbAllAlbs);
this.Controls.Add(this.cbMatchCase);
this.Controls.Add(this.bSearch);
this.Controls.Add(this.lbPSelect);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "SearchDialog";
this.Text = "Search";
this.Closed += new System.EventHandler(this.SearchDialog_FormClosed);
this.gbWhat.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private void cbAllAlbs_CheckedChanged(object sender, EventArgs e) {
if (cbAllAlbs.Checked)
tvAlbums.Enabled = false;
else
tvAlbums.Enabled = true;
}
private void bSearch_Click(object sender, EventArgs e) {
if (!Searching) {
if (tbSearchText.Text.Length == 0)
return;
Search.SearchType sType = new Search.SearchType();
sType.MatchCase = cbMatchCase.Checked;
sType.Title = cbTitle.Checked;
sType.Description = cbDesc.Checked;
sType.TimeTaken = cbDesc.Checked;
sType.Path = cbPath.Checked;
Albums list = BuildAlbumsList();
string[] keywords = GetKeywords();
bSearch.Text = LsAbort;
pbAnim.Visible = true;
lvResults.Items.Clear();
Search s = new Search(this, keywords, list, sType);
s.ResultUpdate += new Search.SearchResultUpdate(s_ResultUpdate);
Searching = true;
s.ExecuteSearch();
EndSearchNow();
} else
EndSearchNow();
}
void s_ResultUpdate(Search.SearchResult sr) {
ListViewItem item = new ListViewItem(new string[] { sr.Photo.Title, sr.Album.AlbumName, sr.Photo.TimeTaken });
item.Tag = sr;
lvResults.Items.Add(item);
lvResults.Invalidate();
}
private string[] GetKeywords() {
string pattern = tbSearchText.Text;
while (pattern[0] == '*')
pattern = pattern.Remove(0, 1);
int wcount = 1;
foreach (char c in pattern) {
if (c == '*')
wcount++;
}
if (pattern[pattern.Length - 1] == '*')
wcount--;
string[] keywords = new string[wcount];
int i, cutLength, lastPos = 0;
wcount = 0;
for (i = 0; i < pattern.Length; i++) {
if (pattern[i] == '*') {
cutLength = i - lastPos;
if (cutLength > 0)
keywords[wcount++] = pattern.Substring(lastPos, cutLength);
lastPos = i + 1;
}
}
if (keywords[0] == null)
keywords[0] = pattern;
wcount = 0;
foreach (string s in keywords) {
if (s != null)
wcount++;
}
string[] keywordsFinal = new string[wcount];
wcount = 0;
for (i = 0; wcount < keywordsFinal.Length; i++) {
if (keywords[i].Length > 0)
keywordsFinal[wcount++] = keywords[i];
}
return keywordsFinal;
}
private Albums BuildAlbumsList() {
if (cbAllAlbs.Checked)
return Albs;
else {
Albums albs = new Albums();
foreach (TreeNode n in tvAlbums.Nodes)
AddAlbumsFromNode(albs, n);
return albs;
}
}
private void AddAlbumsFromNode(Albums albs, TreeNode node) {
if (node.Checked)
albs.Add(node.Tag as Album);
foreach (TreeNode n in node.Nodes)
AddAlbumsFromNode(albs, n);
}
private void tbSearchText_KeyUp(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter)
this.bSearch_Click(null, null);
}
private void lvResults_SelectedIndexChanged(object sender, EventArgs e) {
if (pbPreview.Image != null)
pbPreview.Image.Dispose();
if (lvResults.SelectedItems.Count > 0) {
Search.SearchResult sr = (Search.SearchResult) lvResults.SelectedItems[0].Tag;
Image img = Bitmap.FromFile(sr.Photo.Thumbnail);
pbPreview.Image = img;
}
}
private void SearchDialog_FormClosed(object sender, EventArgs e) {
EndSearchNow();
if (pbPreview.Image != null)
pbPreview.Image.Dispose();
}
private void lvResults_MouseUp(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right)
contMenu.Show(lvResults, new Point(e.X, e.Y));
}
private void contMenu_Popup(object sender, EventArgs e) {
bool enabled;
if (lvResults.SelectedIndices.Count == 0)
enabled = false;
else
enabled = true;
miOpenAlbum.Enabled = enabled;
miOpenPhoto.Enabled = enabled;
miPInfo.Enabled = enabled;
miPrint.Enabled = enabled;
}
private void miPrint_Click(object sender, EventArgs e) {
Search.SearchResult sr = (Search.SearchResult) lvResults.SelectedItems[0].Tag;
Photos ps = new Photos();
ps.Add(sr.Photo);
Printer p = new Printer(ps);
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(p.Print));
t.Start();
}
private void miOpenPhoto_Click(object sender, EventArgs e) {
Search.SearchResult sr = (Search.SearchResult) lvResults.SelectedItems[0].Tag;
PhotoEditor pe = new PhotoEditor(sr.Album.Photos, sr.Album.Photos.IndexOf(sr.Photo), PPane);
pe.Show();
}
private void miOpenAlbum_Click(object sender, EventArgs e) {
Search.SearchResult sr = (Search.SearchResult) lvResults.SelectedItems[0].Tag;
AlbsPane.FireOpenAlbum(sr.Album);
}
private void miPInfo_Click(object sender, EventArgs e) {
Search.SearchResult sr = (Search.SearchResult) lvResults.SelectedItems[0].Tag;
ImageInfoDialog iid = new ImageInfoDialog(sr.Album, sr.Album.Photos.IndexOf(sr.Photo));
iid.ShowDialog();
}
private void lvResults_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter) {
if (lvResults.SelectedItems.Count > 0)
miOpenPhoto_Click(null, null);
}
}
private void lvResults_DoubleClick(object sender, EventArgs e) {
if (lvResults.SelectedItems.Count > 0)
miOpenPhoto_Click(null, null);
}
private void lvResults_MouseMove(object sender, MouseEventArgs e) {
MousePos.X = e.X;
MousePos.Y = e.Y;
}
private void tToolTip_Tick(object sender, EventArgs e) {
ListViewItem item = lvResults.GetItemAt(MousePos.X, MousePos.Y);
if (item != null) {
int itemIndex = item.Index;
if (itemIndex != CurrItemIndex) {
string information;
Search.SearchResult sr = (Search.SearchResult) lvResults.Items[itemIndex].Tag;
PhotoInfo pi = new PhotoInfo(sr.Album, sr.Album.Photos.IndexOf(sr.Photo));
information = String.Concat(pi.PhotoTitle, '\n', pi.ImageType, '\n', pi.FileSizeStr, '\n', pi.ImageWidth.ToString(), " x ", pi.ImageHeight.ToString(), ' ', LsPixels, '\n', pi.TimeTaken);
ttPhotoInfo.SetToolTip(lvResults, information);
CurrItemIndex = itemIndex;
}
} else {
CurrItemIndex = -1;
ttPhotoInfo.RemoveAll();
}
}
private void miCheckAll_Click(object sender, EventArgs e) {
foreach (TreeNode n in tvAlbums.Nodes)
ChangeCheckedState(n, true);
}
private void miUncheckAll_Click(object sender, EventArgs e) {
foreach (TreeNode n in tvAlbums.Nodes)
ChangeCheckedState(n, false);
}
private void tvAlbums_MouseUp(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right)
contMenuTv.Show(tvAlbums, new Point(e.X, e.Y));
}
private void contMenuTv_Popup(object sender, EventArgs e) {
bool enabled;
if (tvAlbums.Nodes.Count == 0)
enabled = false;
else
enabled = true;
miCheckAll.Enabled = enabled;
miUncheckAll.Enabled = enabled;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -